Python convert epoch time to day of the week

天大地大妈咪最大 提交于 2020-02-24 05:23:46

问题


How can I do this in Python? I just want the day of the week to be returned.

>>> convert_epoch_time_to_day_of_the_week(epoch_time_in_miliseconds)
>>> 'Tuesday'

回答1:


ep =  1412673904406

from datetime import datetime

print datetime.fromtimestamp(ep/1000).strftime("%A")
Tuesday


def ep_to_day(ep):
    return datetime.fromtimestamp(ep/1000).strftime("%A")



回答2:


from datetime import date

def convert_epoch_time_to_day_of_the_week(epoch_time_in_miliseconds):
    d = date.fromtimestamp(epoch_time_in_miliseconds / 1000)
    return d.strftime('%A')

Tested, returned Tuesday.




回答3:


If you have milliseconds, you can use the time module:

import time
time.strftime("%A", time.gmtime(epoch/1000))

It returns:

'Tuesday'

Note we use %A as described in strftime:

time.strftime(format[, t])

%A Locale’s full weekday name.


As a function, let's convert the miliseconds to seconds:

import time

def convert_epoch_time_to_day_of_the_week(epoch_milliseconds):
    epoch = epoch_milliseconds / 1000
    return time.strftime("%A", time.gmtime(epoch))

Testing...

Today is:

$ date +"%s000"
1412674656000

Let's try another date:

$ date -d"7 Jan 1993" +"%s000"
726361200000

And we run the function with these values:

>>> convert_epoch_time_to_day_of_the_week(1412674656000)
'Tuesday'
>>> convert_epoch_time_to_day_of_the_week(726361200000)
'Wednesday'



回答4:


import time

epoch = 1496482466
day = time.strftime('%A', time.localtime(epoch))
print day

>>> Saturday


来源:https://stackoverflow.com/questions/26232658/python-convert-epoch-time-to-day-of-the-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!