The datetime class in the datetime module:
Get the current time: datetime.now() current operating system time zone time, date.utctime (UTC time)
Convert to timestamp: timestamp() has nothing to do with the specific time zone
Convert timestamp to datetime: datetime.fromtimestamp()
datetime.utcfromtimestamp
Convert str to datetime: datetime.strptime
('2016-10-01 00:030:00','%Y-%m-%d %H:%M:%S')
Convert datetime to str:datetime.strftime(datetime.now(),'%a ,%b %c %H:%M)
Addition and subtraction of dates: datetime.now() + timedelta(days= 1 ,hours =1)
Convert local time to UTC time:
>>> print(datetime.now().replace(tzinfo=timezone(timedelta(hours=8))))
2018-04-17 11:52:09.414200+08:00
Time zone conversion:
We can first utcnow()
get the current UTC time and then convert it to any time zone:
>>> utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
>>> print(utc_dt)
2018-04-17 03:54:14.380200+00:00
>>> bjutc_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
>>> bjutc_dt
datetime.datetime(2018, 4, 17, 11, 54, 14, 380200, tzinfo=datetime.timezone(date
time.timedelta(0, 28800)))
>>> print(bjutc_dt)
2018-04-17 11:54:14.380200+08:00