码迷,mamicode.com
首页 > 其他好文 > 详细

时间戳,日期,string互转

时间:2019-12-12 19:59:25      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:for   float   str   ptime   pre   local   imp   localtime   int   

import time
import datetime
import operator
def makeStamp(y,m,d,format=‘%Y-%m-%d‘):
"""make timestamp from date """
date_tuple=str(datetime.date(y, m, d))
print(date_tuple)
stamp=time.mktime(time.strptime(date_tuple,format))
return stamp

def get_date(stamp,format="%Y:%m:%d") ->str:
"""get string date by timestamp """
return time.strftime(format,time.localtime(stamp))


def gen_stamp(y,m,d):
""":return stamp: 1567267200.0 """
date=datetime.date(y,m,d)
stamp_date=time.mktime(date.timetuple())
print(stamp_date)
return stamp_date


def str_to_date(date_str)->datetime:
"""return date:2019-01-30,input format:%Y-%m-%d or %Y/%m/%d,but need replace"""
dates=datetime.datetime.strptime(date_str, ‘%Y-%m-%d‘).date()
# print(dates.timetuple())
return dates

def genstamp_toy(date_str)->float:
""" input format :2019-01-03"""
tp=str_to_date(date_str).timetuple()
return time.mktime(tp)

def cmp_le(a,b) ->bool:
c,d=genstamp_toy(a),genstamp_toy(b)
return True if operator.le(c,d) else False

def cmp_ge(a,b) ->bool:
c, d = genstamp_toy(a), genstamp_toy(b)
return True if operator.ge(c, d) else False


def ot():
y=2019
m=9
d=9
dtime = datetime.date(y,m,d)
un_time = time.mktime(dtime.timetuple())
# 将unix时间戳转换为“当前时间”格式
times = time.strftime("%Y/%m/%d", time.localtime(un_time))
#times = datetime.datetime.fromtimestamp(un_time)
print(times,type(times))

时间戳,日期,string互转

标签:for   float   str   ptime   pre   local   imp   localtime   int   

原文地址:https://www.cnblogs.com/SunshineKimi/p/12031052.html

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