Python - Checking times and dates

In Python, you can use a function called datetime.now() to retrieve the current date and time. First you need to import the datetime library so that it can be used in the script, which can be done with from datetime import datetime. You can then display the current date and time with print datetime.now(). E.g.:

Learn Python, it's CAKE1x1 px
$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> 
>>> print datetime.now()
2016-06-22 12:34:06.719688
>>>

You can also use datetime.today().

>>> print datetime.today()
2016-06-22 14:31:26.736321
>>>

The time is in hours, minutes, seconds, and fractions of a second.

If you just want to see the current date, you can use the following code to extract the date:

>>> from datetime import datetime
>>> print str(datetime.now())[0:10]
2016-06-22
>>>

str(datetime.now()) converts the date and time value to a string. The [0:10] that follows extracts just the first ten characters. Counting of character positions starts with zero so The "2" in "2016" is at position 0 and the second "2" in "2" is at position 9. The [0:10] extracts the characters from position 0 up to, but not including 10.

If you just want the time, you can use the following code:

>>> print str(datetime.now())[11:19]
12:53:32
>>>

Or, if you just want the year, month, or day, you can add .day,.month, or .year as shown below, rather than specifying the character range to extract. E.g.

>>> print datetime.now().day
22
>>> thisday = datetime.now()
>>> print thisday.year
2016
>>> print thisday.month
6
>>> print thisday.day
22

If you need to know the day of the week for a date, e.g., Monday, Tuesday, Wednesday, etc., you can add .weekday.

date.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday.

date.isoweekday()
Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a Wednesday.

E.g.:

>>> print datetime.today()
2016-06-22 14:50:44.952305
>>> print datetime.now().weekday()
2
>>> print datetime.now().isoweekday()
3
>>> 
>>> print datetime(2016,06,24).weekday()
4
>>> print datetime(2016,06,24).isoweekday()
5
>>>

There is also a timedelta object in the datetime module. If you import it as well, you can determine a particular date and time that is a certain number of weeks, days, or hours from another date and time. E.g.:

Generic Category (English)120x600
>>> from datetime import datetime, timedelta
>>> print datetime.now()
2016-06-22 13:19:20.196783
>>> print datetime.now() + timedelta(hours=25)
2016-06-23 14:19:43.204564
>>> print datetime.now() + timedelta(days=3)
2016-06-25 13:21:05.377745
>>> print datetime.now() + timedelta(weeks=1)
2016-06-29 13:23:45.572960
>>>

The delta value doesn't have to be a positive value; you can use negative values to determine dates and times in the past. E.g.

>>> print datetime.now()
2016-06-22 13:28:35.924283
>>> print datetime.now() + timedelta(days=-365)
2015-06-23 13:29:20.762302
>>> print datetime.now() + timedelta(hours=-5)
2016-06-22 08:29:39.698231
>>> print datetime.now() + timedelta(weeks=-3)
2016-06-01 13:30:00.193064
>>>

If you want to know how many days have elapsed from a particular day, you can use commands similar to the ones below:

>>> print datetime.now() - datetime(2013,12,31)
904 days, 14:25:26.909957
>>> print datetime(2016,06,22) - datetime(2013,12,31)
904 days, 0:00:00
>>>

You can also of course, use similar commands to determine the number of days between any two dates.

>>> print datetime(2019,4,14) - datetime(2016,8,22)
965 days, 0:00:00
>>> print datetime(2016,8,22) - datetime(2013,12,31)
965 days, 0:00:00

To eliminate the display of hours, minutes, and seconds, the command can be formatted as follows, instead:

>>> print (datetime(2016,06,22) - datetime(2013,12,31)).days
904
>>> print (datetime.now() - datetime(2013,12,31)).days
904
>>>

You can also use Python's strftime directive to format the display of date and time information. E.g., if I wanted to know what date is 1,000 days from December 31, 2013, I could display the date as shown below. In the first case, hours, minutes, and seconds are displayed when I don't apply any formatting control. But, using strftime, I can display only the date plus control how the date is formatted when it is displayed.

>>> days = datetime(2013,12,31) + timedelta(days=1000)
>>> print days
2016-09-26 00:00:00
>>> days.strftime('%m/%d/%Y')
'09/26/2016'
>>> days.strftime('%m/%d/%y')
'09/26/16'
>>> days.strftime('%Y-%m-%d')
'2016-09-26'

The directives used above are as follows (other directives are also available):

DirectiveMeaning
%dDay as a two-digit number
%mMonth as a two-digit number
%yYear as a two-digit number
%YYear as a four-digit number

I can put slashes or dashes between the parts of the date, depending on how I want it to appear.

Related:

Determining the number of days from or to a given date with Python

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px