user1403483
user1403483

Reputation:

Python convert epoch time to day of the week

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'

Upvotes: 6

Views: 13161

Answers (4)

sunil chikkud
sunil chikkud

Reputation: 51

import time

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

>>> Saturday

Upvotes: 5

mccc
mccc

Reputation: 2454

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.

Upvotes: 3

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

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

Upvotes: 9

fedorqui
fedorqui

Reputation: 289835

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'

Upvotes: 2

Related Questions