0

How can I get the specific day number, specific hour, specific minute, and specific second from unix timestmap in python?

yusuf
  • 3,591
  • 8
  • 45
  • 86

3 Answers3

3

It's can be done pretty easy using python datetime module

from datetime import datetime
timestamp = 1456741175
dt = datetime.fromtimestamp(timestamp)
print(dt.day)
# Week day number
print(dt.weekday())
print(dt.minute)
print(dt.second)

Use pytz module if you work with timezone aware timestamps

Nikolai Golub
  • 3,327
  • 4
  • 31
  • 61
1

Assuming a timestamp that is the number of seconds since Jan 1, 1970:

import time
from datetime import datetime

timestamp = time.time()
utc_dt = datetime.utcfromtimestamp(timestamp)    # UTC time
dt = datetime.fromtimestamp(timestamp)           # local time

>>> print(utc_dt)
2016-02-29 10:27:34.753077
>>> print(dt)
2016-02-29 21:27:34.753077
>>> print(datetime.utcfromtimestamp(0))
1970-01-01 00:00:00
>>> print(datetime.fromtimestamp(0))
1970-01-01 10:00:00

You can extract the individual fields using the attributes of the datetime object using dt.day, dt.hour, dt.minute, etc.

mhawke
  • 84,695
  • 9
  • 117
  • 138
1

You can try the following:

import datetime

time_stamp = 1456741030
date_time = datetime.datetime.utcfromtimestamp(time_stamp)
print(date_time.year)    # 2016
print(date_time.month)   # 2
print(date_time.day)     # 29
print(date_time.hour)    # 10
print(date_time.minute)  # 17
print(date_time.second)  # 10
Ziya Tang
  • 91
  • 3