码迷,mamicode.com
首页 > 编程语言 > 详细

python 常用时间获取方法

时间:2017-12-21 11:52:10      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:class   int   orm   mktime   name   log   imp   col   amp   

代码如下:

 1 import datetime
 2 import time
 3 
 4 DATETIME_FORMAT = "%Y-%m-%d  %H:%M:%S"
 5 DATE_FORMAT = "%Y-%m-%d"
 6 
 7 def get_now_timestamp_13():
 8     return int(time.time()*1000)
 9 
10 def get_now_date():
11     return time.strftime(DATE_FORMAT)
12 
13 # 使用datetime.date.today()返回的类型是datetime类型,需要将它变为字符串型
14 def get_yesterday():
15     today = datetime.date.today()
16     return str(today - datetime.timedelta(days=1))
17 
18 def get_now_datetime():
19     return time.strftime(DATETIME_FORMAT)
20 
21 def timestamp_to_datetime(timestamp):
22     # 时间戳转日期时间格式  都是先转为一个下面一样的tuple,再通过strftime(format,tuple)转为datetime,通过strptime(datetime,format)转为时间戳
23     # time.struct_time(tm_year=2017, tm_mon=12, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=354,tm_isdst=-1)
24     return time.strftime(DATETIME_FORMAT,time.localtime(timestamp/1000))
25 
26 def datetime_to_timestamp_13(datetime):
27     return int(time.mktime(time.strptime(datetime,DATETIME_FORMAT))*1000)
28 
29 def datetime_to_timestamp_10(datetime):
30     return int(time.mktime(time.strptime(datetime,DATETIME_FORMAT)))
31 
32 
33 if __name__ == __main__:
34     # print(time.localtime())
35     # print(time.strptime("2017-12-20","%Y-%m-%d"))
36     # print(time.mktime(time.strptime("2017-12-20","%Y-%m-%d")))
37     # print(time.localtime(1513699200))
38     # print(1513699210789/1000)
39     # print(time.strftime("%Y-%m-%d %H-%M-%S",time.localtime(1513699210789/1000)))
40 
41     # print(timestamp_to_datetime(get_now_timestamp_13()))
42     # print(datetime_to_timestamp(timestamp_to_datetime(get_now_timestamp_13())))
43     # print(get_now_timestamp_13())
44     print(get_now_date())
45     print(get_now_datetime())

 

python 常用时间获取方法

标签:class   int   orm   mktime   name   log   imp   col   amp   

原文地址:http://www.cnblogs.com/zipon/p/8078743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!