Convert a epoch timestamp to yyyy/mm/dd hh:mm

后端 未结 1 1504
清酒与你
清酒与你 2021-02-13 18:15

I\'m given a timestamp (time since the epoch) and I need to convert it into this format:

yyyy/mm/dd hh:mm

I looked around and it seems like everyon

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 19:07

    Using datetime instead of dateutil:

    import datetime as dt
    dt.datetime.utcfromtimestamp(seconds_since_epoch).strftime("%Y/%m/%d %H:%M")
    

    An example:

    import time
    import datetime as dt
    
    epoch_now = time.time()
    sys.stdout.write(str(epoch_now))
    >>> 1470841955.88
    
    frmt_date = dt.datetime.utcfromtimestamp(epoch_now).strftime("%Y/%m/%d %H:%M")
    sys.stdout.write(frmt_date)
    >>> 2016/08/10 15:09
    

    EDIT: strftime() used, as the comments suggested.

    0 讨论(0)
提交回复
热议问题