• 时间戳,日期,string互转


    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))




    #####################
    import  datetime
    print(datetime.datetime.fromtimestamp(1614528000)) # 时间戳转日期

    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    # 字符串转时间戳
    datetime_object=datetime.datetime.strptime('2019-09-11 11:20:06', '%Y-%m-%d %H:%M:%S')
    print(datetime_object.timestamp())
    # 时间戳直接转字符串
    date_string=datetime.datetime.fromtimestamp(1614528000).strftime('%Y-%m-%d %H:%M:%S')
    print(date_string)




  • 相关阅读:
    ajax
    vue 思維導圖
    python项目_log日志的使用
    mysql数据库_serialisers
    常见的时间复杂度及其增长速度比较
    C++中的sort()函数
    用C++实现输入三个整数,中间用逗号隔开
    python——递归函数
    python——函数
    python——可变对象和不可变对象
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12031052.html
Copyright © 2020-2023  润新知