Python日期转换常用方法(datetime、time)

x = datetime(2012, 11, 19, 23, 30, 59)
print(x)

x = datetime.strptime('2012-11-19', '%Y-%m-%d')
print(x)
# datetime -> ts
time_time = time.mktime(datetime.today().timetuple())
print(time_time)

# ts -> string
time.strftime('%Y-%m-%d', time.localtime(time_time))
# datetime -> string
now_str = datetime.today().strftime('%Y-%m-%d')
print(now_str)

x = datetime.strptime(now_str, '%Y-%m-%d')
print(x)
# date -> datetime
x = date(2012, 11, 19)
print(x)

你可能感兴趣的:(Python)