Skip to content

Instantly share code, notes, and snippets.

@korakot
Created October 30, 2017 13:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korakot/ccd8970c5eaa3f82f8a7cc4f6c36f9ac to your computer and use it in GitHub Desktop.
Save korakot/ccd8970c5eaa3f82f8a7cc4f6c36f9ac to your computer and use it in GitHub Desktop.
Thai datetime in python
import datetime, pytz
tz = pytz.timezone('Asia/Bangkok')
def now():
now1 = datetime.datetime.now(tz)
month_name = 'x มกราคม กุมภาพันธ์ มีนาคม เมษายน พฤษภาคม มิถุนายน กรกฎาคม สิงหาคม กันยายน ตุลาคม พฤศจิกายน ธันวาคม'.split()[now1.month]
thai_year = now1.year + 543
time_str = now1.strftime('%H:%M:%S')
return "%d %s %d %s"%(now1.day, month_name, thai_year, time_str) # 30 ตุลาคม 2560 20:45:30
@korakot
Copy link
Author

korakot commented Nov 11, 2018

tz.localize(d) # change unaware→ aware

d.replace(tzinfo=None) # back to unaware
d.aztimezone(tz) # change timezone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment