Python จัดการกับวันที่และเวลา

ใน Python การจัดการวันที่และเวลาเป็นเรื่องสำคัญที่สามารถทำได้โดยใช้โมดูลหลักอย่าง datetime และยังมีโมดูลอื่น ๆ เช่น time และ calendar ที่สามารถใช้งานได้สำหรับงานเฉพาะทาง

โมดูล datetime

โมดูลนี้เป็นเครื่องมือหลักในการจัดการวันที่และเวลาใน Python โดยมีคลาสย่อยที่สำคัญได้แก่ datetime, date, time, timedelta และ timezone

คลาส date

ใช้สำหรับจัดการกับวันที่เท่านั้น (ไม่รวมเวลา)

ตัวอย่างการใช้งาน

Python
from datetime import date

# สร้างวันที่ปัจจุบัน
today = date.today()
print("Today's date:", today)

# สร้างวันที่แบบกำหนดเอง
my_birthday = date(1995, 6, 15)
print("My birthday:", my_birthday)

# แยกส่วนปี เดือน วัน
print("Year:", today.year)
print("Month:", today.month)
print("Day:", today.day)

คลาส time

ใช้สำหรับจัดการกับเวลาเพียงอย่างเดียว (ไม่รวมวันที่)

ตัวอย่างการใช้งาน

Python
from datetime import time

# สร้างเวลา
my_time = time(14, 30, 15)
print("Time:", my_time)

# แยกส่วนชั่วโมง นาที วินาที
print("Hour:", my_time.hour)
print("Minute:", my_time.minute)
print("Second:", my_time.second)

คลาส datetime

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

ตัวอย่างการใช้งาน

Python
from datetime import datetime

# วันที่และเวลาปัจจุบัน
now = datetime.now()
print("Now:", now)

# สร้าง datetime แบบกำหนดเอง
custom_datetime = datetime(2023, 9, 28, 14, 30, 15)
print("Custom datetime:", custom_datetime)

# แยกส่วนต่าง ๆ
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)

การเปรียบเทียบและคำนวณระยะเวลา (timedelta)

ใช้คลาส timedelta เพื่อคำนวณความแตกต่างระหว่างวันที่หรือเวลา เช่น การบวกหรือลบวัน

ตัวอย่างการใช้งาน

Python
from datetime import timedelta

# คำนวณระยะเวลา
tomorrow = today + timedelta(days=1)
print("Tomorrow's date:", tomorrow)

# คำนวณความแตกต่างระหว่างสองวันที่
date_diff = tomorrow - today
print("Difference between dates:", date_diff)

การแปลงรูปแบบวันที่และเวลา

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

การแปลงจาก datetime เป็นสตริง (strftime)

Python
# แปลง datetime เป็นสตริง
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted datetime:", formatted_date)

การแปลงจากสตริงเป็น datetime (strptime)

Python
# แปลงสตริงเป็น datetime
date_string = "2023-09-28 14:30:15"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed datetime:", parsed_date)

โมดูล time

โมดูลนี้ใช้สำหรับจัดการกับเวลาที่เป็นรูปแบบ UNIX timestamp ซึ่งเป็นจำนวนวินาทีที่นับจากวันที่ 1 มกราคม ค.ศ. 1970

ตัวอย่างการใช้งาน

Python
import time

# เวลาปัจจุบันในรูปแบบ timestamp
timestamp = time.time()
print("Current timestamp:", timestamp)

# แปลง timestamp เป็น struct_time
struct_time = time.localtime(timestamp)
print("Local time:", struct_time)

# แปลง struct_time เป็น timestamp
new_timestamp = time.mktime(struct_time)
print("Timestamp from struct_time:", new_timestamp)

โมดูล calendar

โมดูลนี้ใช้สำหรับการทำงานที่เกี่ยวข้องกับปฏิทิน

ตัวอย่างการใช้งาน

Python
import calendar

# ดูปฏิทินของเดือน
print(calendar.month(2023, 9))

# ตรวจสอบปีที่เป็น leap year
is_leap = calendar.isleap(2024)
print("Is 2024 a leap year?", is_leap)

สรุป

  • โมดูล datetime เป็นเครื่องมือหลักในการจัดการวันที่และเวลา
  • ใช้ timedelta เพื่อคำนวณระยะเวลาระหว่างวันที่
  • ใช้ strftime() และ strptime() เพื่อแปลงรูปแบบวันที่
  • โมดูล time ใช้จัดการกับ timestamp
  • โมดูล calendar ใช้ในการทำงานที่เกี่ยวกับปฏิทิน
แชร์เรื่องนี้