bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python Datetime

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python Datetime?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ datetime
3Order

Put the learning moves in the order that makes the concept easiest to apply.

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
The strftime() Method
Creating Date Objects

Python Dates

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.

Example

import datetime
x = datetime.datetime.now()
print(x)

Date Output

When we execute the code from the example above the result will be:

The date contains year, month, day, hour, minute, second, and microsecond.

The datetime module has many methods to return information about the date object.

Here are a few examples, you will learn more about them later in this chapter:

Example

import datetime
x = datetime.datetime.now()
print(x.year)

print(x.strftime("%A"))

Creating Date Objects

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.

Example

import datetime
x = datetime.datetime(2020, 5, 17)

print(x)

The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0 , ( None for timezone).

The strftime() Method

The datetime object has a method for formatting date objects into readable strings.

The method is called strftime() , and takes one parameter, format , to specify the format of the returned string:

Example

import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))

A reference of all the legal format codes:

DirectiveDescriptionExample
%aWeekday, short versionWed
%AWeekday, full versionWednesday
%wWeekday as a number 0-6, 0 is Sunday3
%dDay of month 01-3131
%bMonth name, short versionDec
%BMonth name, full versionDecember
%mMonth as a number 01-1212
%yYear, short version, without century18
%YYear, full version2018
%HHour 00-2317
%IHour 00-1205
%pAM/PMPM
%MMinute 00-5941
%SSecond 00-5908
%fMicrosecond 000000-999999548513
%zUTC offset+0100
%ZTimezoneCST
%jDay number of year 001-366365
%UWeek number of year, Sunday as the first day of week, 00-5352
%WWeek number of year, Monday as the first day of week, 00-5352
%cLocal version of date and timeMon Dec 31 17:41:00 2018
%CCentury20
%xLocal version of date12/31/18
%XLocal version of time17:41:00
%%A % character%
%GISO 8601 year2018
%uISO 8601 weekday (1-7)1
%VISO 8601 weeknumber (01-53)01

Previous

Python Modules

Next

Python Math