from datetime import datetime, timezone, timedelta tz = timezone(timedelta(hours=+8)) fmt = '%Y-%m-%dT%H:%M:%S.%f%z' zoned_time1 = datetime.today().astimezone(tz) print(1, zoned_time1.strftime(fmt)) #1 2023-01-03T09:33:27.074210+0800 zoned_time2 = datetime.now(tz) print(2, zoned_time2.strftime(fmt)) #2 2023-01-03T09:33:27.075036+0800 zoned_time3 = datetime.utcnow() print(3, zoned_time3.isoformat()) #3 2023-01-03T01:33:27.075785 print(4, zoned_time2.strftime('%Y-%m-%dT%H:%M:%S.%f%Z')) #4 2023-01-03T09:33:27.075036UTC+08:00 timestamp = datetime.today().timestamp() print(5, timestamp) #5 1672709607.077365 print(6, datetime.fromtimestamp(timestamp, tz=tz)) #6 2023-01-03 09:33:27.077365+08:00 zoned_time4 = datetime(2023, 1, 3, 9, 31, 34, 345, tzinfo=tz) print(7, zoned_time4.isoformat()) #7 2023-01-03T09:31:34.000345+08:00 print(8, zoned_time2.isoformat()) #8 2023-01-03T09:33:27.075036+08:00
标签:01,tz,zoned,Python,datetime,处理,2023,print,时区 From: https://www.cnblogs.com/boye169/p/17021105.html