格式化输出当前时间
>>> import time >>> print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 2017-08-21 10:54:31 >>>
张小川
格式化输出当前时间
>>> import time >>> print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 2017-08-21 10:54:31 >>>
分享一个获取含毫秒的当前时间的方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- import time def get_current_time(): """[summary] 获取当前时间 [description] 用time.localtime()+time.strftime()实现 :returns: [description] 返回str类型 """ ct = time.time() local_time = time.localtime(ct) data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time) data_secs = (ct - long(ct)) * 1000 time_stamp = "%s.%03d" % (data_head, data_secs) return time_stamp print get_current_time()
输出结果类似如下:
2018-06-04 19:06:32.641
Citizen_Wang
实例,python3 环境计算某天是当年的第几天。
#!/usr/bin/python # -*- coding: UTF-8 -*- date = input("输入时间(格式如:2017-04-04):") t = time.strptime(data,"%Y-%m-%d") print(time.strftime("今年的第%j天",t))