Python - Checking times and dates

In Python, you can use a function called 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 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
2016-06-22 12:34:06.719688

You can also use

>>> print
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([0:10]

str( 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([11:19]

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
>>> thisday =
>>> print thisday.year
>>> print thisday.month
>>> print

If you need to know the day of the week for a date, e.g., Monday, Tuesday, Wednesday, etc., you can add .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.

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.


>>> print
2016-06-22 14:50:44.952305
>>> print
>>> print
>>> print datetime(2016,06,24).weekday()
>>> print datetime(2016,06,24).isoweekday()

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
2016-06-22 13:19:20.196783
>>> print + timedelta(hours=25)
2016-06-23 14:19:43.204564
>>> print + timedelta(days=3)
2016-06-25 13:21:05.377745
>>> print + 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
2016-06-22 13:28:35.924283
>>> print + timedelta(days=-365)
2015-06-23 13:29:20.762302
>>> print + timedelta(hours=-5)
2016-06-22 08:29:39.698231
>>> print + 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(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
>>> print ( - datetime(2013,12,31)).days

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')
>>> days.strftime('%m/%d/%y')
>>> days.strftime('%Y-%m-%d')

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

%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.


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


TechRabbit ad 300x250

Justdeals Daily Electronics Deals1x1 px