Question: Convert datetime to long number

Gerrit Holl gerrit at nl.linux.org
Sun May 25 05:39:51 EDT 2003


John Smith schreef op zondag 25 mei om 04:22:33 +0000:
> HI,
>   A probably very simple question. How do I convert a datetime object to the
> long number.
> I mean if I do
> 
> >>> x=datetime.utcnow(); a=time.time()
> >>> x,a
> (datetime.datetime(2003, 5, 25, 2, 2, 32, 540000), 1053828152.54)
> >>>
> 
> what function f I can apply to x such that  f(x) will return 1053828152.54 ?

You can use the strftime method, and pass "%s" as entry:

datetime.datetime.utcnow().strftime("%s")

Note that strftime returns an int, not a float.

See also:

http://www.python.org/dev/doc/devel/lib/datetime-datetime.html#l2h-1641
http://www.python.org/dev/doc/devel/lib/module-time.html#l2h-1673

yours,
Gerrit.

-- 
188. If an artizan has undertaken to rear a child and teaches him his
craft, he can not be demanded back. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list