Python โมดูล datetime

โมดูล datetime ใน Python เป็นเครื่องมือที่ใช้ในการจัดการเกี่ยวกับวันที่และเวลาได้อย่างมีประสิทธิภาพ โดยโมดูลนี้มีคลาสหลักๆ ที่สำคัญ ได้แก่ datetime, date, time, และ timedelta ซึ่งเราสามารถใช้เพื่อจัดการการคำนวณเวลา เปรียบเทียบ หรือปรับเปลี่ยนรูปแบบของวันที่และเวลาได้

การนำเข้าโมดูล datetime

Python
import datetime

การใช้งานคลาส datetime

คลาสนี้ใช้สำหรับการทำงานกับทั้งวันที่และเวลา

การสร้างวัตถุ datetime

Python
import datetime

# การสร้าง datetime ปัจจุบัน
current_datetime = datetime.datetime.now()
print(current_datetime)

# การสร้าง datetime แบบระบุเอง
custom_datetime = datetime.datetime(2023, 9, 28, 10, 45, 0)  # ปี, เดือน, วัน, ชั่วโมง, นาที, วินาที
print(custom_datetime)

การเข้าถึงองค์ประกอบของ datetime

Python
print("ปี:", current_datetime.year)
print("เดือน:", current_datetime.month)
print("วัน:", current_datetime.day)
print("ชั่วโมง:", current_datetime.hour)
print("นาที:", current_datetime.minute)
print("วินาที:", current_datetime.second)

การปรับเปลี่ยนรูปแบบการแสดงผล datetime

ใช้ฟังก์ชัน strftime() เพื่อเปลี่ยนรูปแบบการแสดงผลวันที่และเวลา

Python
formatted_datetime = current_datetime.strftime("%d-%m-%Y %H:%M:%S")
print(formatted_datetime)  # รูปแบบที่ได้จะเป็น วัน-เดือน-ปี ชั่วโมง:นาที:วินาที

การแปลงสตริงเป็น datetime

ใช้ฟังก์ชัน strptime() เพื่อแปลงสตริงให้เป็นวัตถุ datetime

Python
date_str = "28-09-2023 10:45:00"
converted_datetime = datetime.datetime.strptime(date_str, "%d-%m-%Y %H:%M:%S")
print(converted_datetime)

การใช้งานคลาส date

คลาสนี้จะทำงานเฉพาะกับวัน เดือน และปีเท่านั้น

การสร้างวัตถุ date

Python
# การสร้าง date ปัจจุบัน
current_date = datetime.date.today()
print(current_date)

# การสร้าง date แบบระบุเอง
custom_date = datetime.date(2023, 9, 28)
print(custom_date)

การปรับเปลี่ยนรูปแบบการแสดงผล date

Python
formatted_date = current_date.strftime("%d-%m-%Y")
print(formatted_date)

การใช้งานคลาส time

คลาสนี้จะทำงานเฉพาะกับเวลา (ชั่วโมง นาที วินาที)

การสร้างวัตถุ time

Python
custom_time = datetime.time(10, 45, 30)  # ชั่วโมง, นาที, วินาที
print(custom_time)

การเข้าถึงองค์ประกอบของ time

Python
print("ชั่วโมง:", custom_time.hour)
print("นาที:", custom_time.minute)
print("วินาที:", custom_time.second)

การใช้งานคลาส timedelta

คลาสนี้ใช้สำหรับการคำนวณเกี่ยวกับช่วงระยะเวลา เช่น การบวกหรือลบวันหรือเวลาจากวันที่หนึ่งไปยังอีกวันที่หนึ่ง

การสร้างวัตถุ timedelta

Python
# การสร้าง timedelta 7 วัน
delta = datetime.timedelta(days=7)

# การบวก timedelta เข้ากับ datetime
new_date = current_datetime + delta
print(new_date)  # วันที่จะถูกเพิ่มขึ้น 7 วัน

การหาความแตกต่างระหว่างสองวัน

Python
date1 = datetime.date(2023, 9, 28)
date2 = datetime.date(2024, 9, 28)

difference = date2 - date1
print(difference.days)  # ผลลัพธ์คือจำนวนวันระหว่างวันทั้งสอง

การใช้งาน Time Zone (เขตเวลา)

Python มีไลบรารี pytz ที่ใช้ในการจัดการกับเขตเวลาที่ต่างกัน

การนำเข้าและใช้งาน pytz

Python
import pytz

# แสดงรายการเขตเวลาที่ใช้ได้
print(pytz.all_timezones)

# การตั้งเขตเวลาให้กับ datetime
timezone = pytz.timezone("Asia/Bangkok")
current_datetime_with_tz = timezone.localize(datetime.datetime.now())
print(current_datetime_with_tz)
แชร์เรื่องนี้