How do I extract -30 days from this time format

Viewed 38

Hello I am working on predicting some values and for that I need to access all values in past 30 months. However in my timedate, there is a value of 1527505282 and I am wondering how to I get an actual date from this. 1527505282 should be equal to 28.05.2018 18:07:30 with format: dd-mm-yyyy hh:mm:ss.

Could anyone please help me understand what format of time is this and how do I get to dd-mm format in python? Thank you

2 Answers

This type of time is called epoch

import time
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1527505282))

or

import datetime 
datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')

For UTC

import datetime
datetime.datetime.utcfromtimestamp(1527505282).strftime('%Y-%m-%d %H:%M:%S')

Since you only want to get 30days data may be you could simply subtract that many days .

in epoch 1 day = 86400 for 30 day = 86400*30 = 2592000 so you could use

timestamp = 1527505282 - 2592000
if timestamp<=1527505282:

SO this would ensure your data is in this range

You can try:

from datetime import datetime
timestamp = 1527505282
date_obj = datetime.fromtimestamp(timestamp)
print(date_obj)
date_str = datetime.strftime(date_obj, "%d-%m")
print(date_str)

Output:

2018-05-28 16:31:22
28-05
Related