paulc1111
paulc1111

Reputation: 651

How to change decimal number of time to date and time in python

I'm a newbie in python and have a simple question in python 2.7. I'm getting 1492500360 this number like in excel for date and time in python and want it to be converted like 2017/04/17 hh:mm:ss.

What kind of method should I use?

I actually looked over in Google, but can't find (I might not be good at searching...)

Thanks in advance.

Upvotes: 0

Views: 71

Answers (3)

NineTail
NineTail

Reputation: 223

The code below uses datetime module. It will then grab the current time from your machine and store it in current_time. From there you can do whatever you want.

from datetime import datetime
current_time = datetime.strftime(datetime.now(), '%Y/%m/%d %H:%M:%S')
print current_time

OUTPUT: 2017/04/18 09:23:01

Upvotes: 0

Don Zhang
Don Zhang

Reputation: 36

That code works:

lt = time.localtime(1492500360)
stime = '%4d/%02d/%02d %02d:%02d:%02d' % (lt.tm_year, lt.tm_mon, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec)

stime is the formatted string we wanted

Upvotes: 2

stepBystep
stepBystep

Reputation: 187

Maybe that could help.

# -*- coding: utf-8 -*-
from datetime import timedelta, datetime

def TimeNow(time):
    sec = timedelta(seconds=int(time))
    dtime = datetime(1,1,1) + sec
    dnow = datetime.now()


    print("YEAR: %d \tDAY: %d \tMONTH: %d \tHOURS: %d \tMIN: %d \tSEC: %d" % (dnow.year, dtime.day, dnow.month, 
                                                                              dtime.hour, dtime.minute, dtime.second))

TimeNow(1492500360)

Upvotes: 0

Related Questions