1

In javascript console ,I tried this

d=new Date()
t=d.getTime()

When I printed t,it gave

>>1334855220112

As per documentation ,it must be the number of milliseconds since 1970/01/01

In python ,I created a datetime.datetime object as

nowdtime=datetime.datetime.today()
print str(int(nowdtime.strftime("%s"))*1000)

This gave the result:

'1334855221000'

This value looks very close to the result I got from javascript console.

the value of nowdtime is

datetime.datetime(2012, 4, 19, 22, 37, 1, 866262)

My doubt is why nowdtime.strftime("%s") returns number of seconds since epoch?

If I give nowdtime.strftime("%H") ,it will only return 22 which is the hour according to 24 hr clock.

2 Answers 2

2

The %s must be some sort of platform specific (ie, not documented at http://docs.python.org/library/time.html#time.strftime) directive:

Additional directives may be supported on certain platforms, but only the ones listed here have a meaning standardized by ANSI C.

Ie, Win7 Python 2.6.6 iPython console:

import datetime
d = datetime.datetime(2012, 4, 19, 22, 37, 1, 866262)
d.strftime("%s") # gives: ''

I'm not sure if this answers your question, because I'm not sure what your question is.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for telling me about the platform specific issue..I was using linux..so didn't know whether it worked on windows. Anyway ,time.mktime(datetime.datetime.now().timetuple()) * 1000 returns the expected milliseconds since epoch.
Yeah, I get your result on Ubuntu.
0

You can also just use the time method from time module time.time() to get a float that indicates the time in seconds since epoch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.