Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: convert date time

Reply
Thread Tools

Re: convert date time

 
 
Kushal Kumaran
Guest
Posts: n/a
 
      08-22-2009
On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<> wrote:
> I'm new to python and I'm getting a date time from a field in the database
> that looks like this:
> 8/2/2009 8:36:16 AM (UTC)
>
> I want to split it into two fields one with the date formatted like this:
> YYYY-MM-DDÂ* 2009-08-02
>
> and the time to be 24 hour or military time. How every you call it. Similar
> to this:
> 15:22:00
>
> I found it easy to truncate off the (UTC), but having trouble converting the
> date. Can someone point me in the right direction?
>


datetime.datetime.strptime() will give you a datetime object, which
you can then format in a wide variety of ways using its strftime()
method. Doesn't work with dates earlier than 1900, I believe.

--
kushal
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      08-22-2009
On Aug 22, 5:11*pm, Kushal Kumaran <kushal.kumaran+pyt...@gmail.com>
wrote:
> On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<ronn.r...@gmail.com> wrote:
> > I'm new to python and I'm getting a date time from a field in the database
> > that looks like this:
> > 8/2/2009 8:36:16 AM (UTC)


> datetime.datetime.strptime() will give you a datetime object, which
> you can then format in a wide variety of ways using its strftime()
> method. *Doesn't work with dates earlier than 1900, I believe.


The datetime module works from 0001-01-01 onwards; see
http://docs.python.org/library/datet...tetime.MINYEAR

Other things worth mentioning:

(1) datetime.datetime.strptime() was introduced in Python 2.5; for
Python 2.3 and 2.4, use time.strptime() [which may not like years
before 1970] followed by datetime.datetime()

(2) as the input has 12-hour clock plus AM/PM, ensure that you use %I
instead of %H for the hour component; %H won't do what you expect if
your expectation is not based on reading the docs
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert date recevied as String to date in local time zone deepak_kamath_n@yahoo.co.in C++ 8 05-01-2007 12:26 PM
w3.org suggestion .. page, date, time and topic, date, time code (wish list). Keith Cochrane HTML 2 08-06-2006 06:57 AM
how can I convert date infomation to a string just includes the date not the time wgan Java 7 07-08-2004 07:08 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date & Time chooser for java 1.1 - using only the mouse to select time & date Chris Berg Java 0 10-27-2003 10:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57