Search code examples
pythondatetimeunix-timestamp

How can I proper date time value from unix time?


UNIX Timestamp

  1262304000
  1224633600
  940636800
  939772800
  1332288000
  1096934400
  1088121600    
   ...

1.Try

frmt_date = dt.datetime.utcfromtimestamp(1088121600).strftime("%Y/%m/%d %H:%M")

Those results of time value were 00:00.

(result)

 2012-10-11 00:00
 2004-04-20 00:00
 1999-01-07 00:00
 ....
  1. Try

     def xldate_to_datetime(xldate):
     temp = dt.datetime(1899, 12, 30)
     delta = dt.timedelta(days=xldate)
     return temp+delta
     ts = "1262304000"
     tsxl = ((int(ts)/60)/60)/24 + 25569
     readabledate =  xldate_to_datetime(tsxl)
     print(readabledate)
    

results of time value were all 00:00, though.

How can I get proper time from those UNIX https://i.sstatic.net/khDmi.jpg


Solution

  • Another approach

    >>> mytime =  1332288000
    >>> import datetime as dt
    >>> frmt_date = dt.datetime.utcfromtimestamp(mytime).strftime("%Y/%m/%d %H:%M")
    >>> print(frmt_date)
    2012/03/21 00:00
    >>> mytime =  mytime + 10000
    >>> frmt_date = dt.datetime.utcfromtimestamp(mytime).strftime("%Y/%m/%d %H:%M")
    >>> print(frmt_date)
    2012/03/21 02:46
    

    So, your date times were indeed at 00:00