Python Date
Dates in Python are not their own datatype, but you can import a module called datetime that treats dates as date objects.
an actual example
Import the datetime module and display the current date:
import datetime x = () print(x)
running example
2023-02-14 20:43:20.889388
date output
If we execute the above code, the result will be:
2019-08-14 12:52:55.817273
The date contains year, month, day, hour, minute, second and microsecond.
The datetime module has a number of methods that return information about the date object.
Here are some examples; you'll learn about them in detail later in this chapter:
an actual example
Returns the name of weekday and the year:
import datetime x = () print() print(("%A"))
running example
2023 Tuesday
Creating Date Objects
To create a date, we can use the datetime() class (constructor) of the datetime module.
The datetime() class takes three arguments to create a date: year, month, and day.
an actual example
Creates a date object:
import datetime x = (2020, 5, 17) print(x)
running example
2020-05-17 00:00:00
The datetime() class also accepts time and timezone (hours, minutes, seconds, microseconds, tzone) arguments, though they are optional and default to 0, (timezone defaults to None).
strftime() method
The datetime object has methods for formatting date objects into readable strings.
This method is called strftime() and uses a format argument to specify the format of the returned string:
an actual example
Displays the name of the month:
import datetime x = (2019, 10, 1) print(("%B"))
running example
October
Reference to all legal format codes:
Directive: %a Description: weekday, short version Example: wed
import datetime x = () print(("%a"))
Run the instance:
Tue
Command: %A Description: weekday, full version Example: Wednesday
import datetime x = () print(("%A"))
Run the instance:
Tuesday
Command: %w Description: Weekday, numbers 0-6, 0 is Sunday Example: 3
import datetime x = () print(("%w"))
Run the instance:
2
Command: %d Description: day, number 01-31 Example: 31
import datetime x = () print(("%d"))
Run the instance:
14
Command: %b Description: month name, short version Example: dec
import datetime x = () print(("%b"))
Run the instance:
Feb
Command: %B Description: month name, full version Example: December
import datetime x = () print(("%B"))
Run the instance:
February
Command: %m Description: month, numbers 01-12 Example: 12
import datetime x = () print(("%m"))
Run the instance:
02
Instructions:%y
Description: Year, short version, no century Example: 18
import datetime x = () print(("%y"))
Run the instance:
23
Command: %Y Description: year, full version Example: 2018
import datetime x = () print(("%Y"))
Run the instance:
2023
Command: %H Description: hour, 00-23 Example: 17
import datetime x = () print(("%H"))
Run the instance:
20
Command: %I Description: hour, 00-12 Example: 05
import datetime x = () print(("%I"))
Run the instance:
08
Command: %p Description: AM/PM Example: PM
import datetime x = () print(("%p"))
Run the instance:
PM
Command: %M Description: minutes, 00-59 Example: 41
import datetime x = () print(("%M"))
Run the instance:
57
Command: %S Description: seconds, 00-59 Example: 08
import datetime x = () print(("%S"))
Run the instance:
02
Instruction: %f Description: subtle, 000000-999999 Example: 548513
import datetime x = () print(("%f"))
Run the instance:
092953
Command: %j Description: number of days, 001-366 Example: 365
import datetime x = () print(("%j"))
Run the instance:
045
Command: %U Description: number of weeks, first day of week is Sunday, 00-53 Example: 52
import datetime x = (2018, 5, 30) print(("%U"))
Command: %W Description: number of weeks, the first day of the week is Monday, 00-53 Example: 52
import datetime x = (2018, 5, 31) print(("%W"))
Run the instance:
22
Command: %c Description: local version of date and time Example: mon dec 31 17:41:00 2018
import datetime x = () print(("%c"))
Run the instance:
Tue Feb 14 21:00:34 2023
Command: %x Description: local version of date Example: 12/31/18
import datetime x = () print(("%x"))
Run the instance:
02/14/23
Command: %X Description: local version of time Example: 17:41:00
import datetime x = () print(("%X"))
Run the instance:
21:01:34
Instruction: %% Description: A % character Example: %
import datetime x = () print(("%%"))
Run the instance:
python_datetime_strftime_percent.py %
To this point this tutorial on Python Getting Started (XXVII) Python date of the article is introduced to this, more related to the date of Python content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!