1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import datetime import time
st = "2017-11-23 16:10:10"
dt = datetime.datetime.now()
sp = time.time()
def datetime_toString(dt): print("1.把datetime转成字符串: ", dt.strftime("%Y-%m-%d %H:%M:%S"))
def string_toDatetime(st): print("2.把字符串转成datetime: ", datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))
def string_toTimestamp(st): print("3.把字符串转成时间戳形式:", time.mktime(time.strptime(st, "%Y-%m-%d %H:%M:%S")))
def timestamp_toString(sp): print("4.把时间戳转成字符串形式: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp)))
def datetime_toTimestamp(dt): print("5.把datetime类型转外时间戳形式:", time.mktime(dt.timetuple()))
datetime_toString(dt)
string_toDatetime(st)
string_toTimestamp(st)
timestamp_toString(sp)
datetime_toTimestamp(dt)
|