Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify date and time in python?

What is the object used in Python to specify date (and time) in Python?

For instance, to create an object that holds a given date and time, (let's say '05/10/09 18:00').

As per S.Lott's request, so far I have:

class Some:
    date =

I stop there. After the "=" sign for, I realize I didn't knew what the right object was ;)

like image 927
OscarRyz Avatar asked Oct 05 '09 19:10

OscarRyz


People also ask

How do you display date and time in Python?

Datetime module comes built into Python, so there is no need to install it externally. To get both current date and time datetime. now() function of DateTime module is used. This function returns the current local date and time.

How do I set datetime in Python?

To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.

How do I code a date in Python?

Example 1: Python get today's date Then, we used the date.today() method to get the current local date. By the way, date.today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

How do I change date and time format in Python?

Function used. strftime() can change the date format in python. Where, format is a string representing the type of required date format.


3 Answers

Simple example:

>>> import datetime
# 05/10/09 18:00
>>> d = datetime.datetime(2009, 10, 5, 18, 00)
>>> print d.year, d.month, d.day, d.hour, d.second
2009 10 5 18 0
>>> print d.isoformat(' ')
2009-10-05 18:00:00
>>> 
like image 85
Nick Dandoulakis Avatar answered Oct 14 '22 02:10

Nick Dandoulakis


Nick D has the official way of handling your problem. If you want to pass in a string like you did in your question, the dateutil module (http://labix.org/python-dateutil) has excellent support for that kind of thing.

For examples, I'm going to copy and paste from another answer I gave a while back now:

Simple example:

>>> parse("Thu Sep 25 2003")
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep 25 2003")
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep 2003", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("Sep", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

>>> parse("2003", default=DEFAULT)
datetime.datetime(2003, 9, 25, 0, 0)

To ambigous:

>>> parse("10-09-2003")
datetime.datetime(2003, 10, 9, 0, 0)

>>> parse("10-09-2003", dayfirst=True)
datetime.datetime(2003, 9, 10, 0, 0)

>>> parse("10-09-03")
datetime.datetime(2003, 10, 9, 0, 0)

>>> parse("10-09-03", yearfirst=True)
datetime.datetime(2010, 9, 3, 0, 0)

To all over the board:

>>> parse("Wed, July 10, '96")
datetime.datetime(1996, 7, 10, 0, 0)

>>> parse("1996.07.10 AD at 15:08:56 PDT", ignoretz=True)
datetime.datetime(1996, 7, 10, 15, 8, 56)

>>> parse("Tuesday, April 12, 1952 AD 3:30:42pm PST", ignoretz=True)
datetime.datetime(1952, 4, 12, 15, 30, 42)

>>> parse("November 5, 1994, 8:15:30 am EST", ignoretz=True)
datetime.datetime(1994, 11, 5, 8, 15, 30)

>>> parse("3rd of May 2001")
datetime.datetime(2001, 5, 3, 0, 0)

>>> parse("5:50 A.M. on June 13, 1990")
datetime.datetime(1990, 6, 13, 5, 50)

Take a look at the documentation for it here:

http://labix.org/python-dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2

like image 45
Eric Palakovich Carr Avatar answered Oct 14 '22 02:10

Eric Palakovich Carr


Look at the datetime module; there are datetime, date and timedelta class definitions.

like image 4
S.Lott Avatar answered Oct 14 '22 03:10

S.Lott