0% found this document useful (0 votes)
40 views5 pages

DateTime For Python

The document discusses various ways to work with dates and times in Python. It introduces the datetime and calendar modules, which allow Python programs to track dates and times. The datetime module represents dates and times using datetime objects, which can be manipulated and formatted. The calendar module provides methods for working with monthly and yearly calendars.

Uploaded by

Mab Shi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
40 views5 pages

DateTime For Python

The document discusses various ways to work with dates and times in Python. It introduces the datetime and calendar modules, which allow Python programs to track dates and times. The datetime module represents dates and times using datetime objects, which can be manipulated and formatted. The calendar module provides methods for working with monthly and yearly calendars.

Uploaded by

Mab Shi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

WORKING WITH DATE AND TIME

Most programs require date and time data to operate. Python


has time and calendar modules that can be used to track times
and dates in programs.
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 the datetime module and display the current date:

Note: Use the IDLE window(as discussed in unit 1) for our


discussions in this unit, Create a new file on Shell window and
type the following:

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

Save the file and run it.

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

The datetime module has many methods to return information


about the date object.

Continue typing on the same file. Add the following codes.


Save then run file.

1
x = datetime.datetime.now()
print(x.year)
print(x.strftime(“%A”))

Output:
2020
Tuesday or the day today

Creating Date time. Add the following codes.

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

Output:
2020-05-17 00:00:00

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
Display the name of the month:

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

Output:
June or the name of the current month

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

Output:
Wed-June

A reference of all the legal format codes:

2
Directive Description Example
%a Weekday, short version Wed
%A Weekday, full version Wednesday
%w Weekday as a number 0-6, 0 is Sunday 3
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version December
%m Month as a number 01-12 12
%y Year, short version 18
%Y Year, full version 2018
%H Hour 00-23 17
%I Hour 00-12 05
%p AM/PM PM
%M Minute 00-59 41
%S Second 00-59 08
%f Microsecond 000000-999999 548513
%j Day number of year 001-366 365
%c Local version of date and time Mon Dec 31 17:41:00
%x Local version of date 12/31/18
%X Local version of time 17:41:00

Formatting Time

You can format time in many different ways with the print
function. There are also several ways to get readable time
format in Python. A simple way to get time is with the
asctime() function. Here’s an example:

import time
time_now = time.asctime( time.localtime())
print("Current date and time is:", time_now)

Output:
Current date and time is: Tue Jul 21 09:39:29 2020

Calendar Module

The calendar module provides several methods that allow users


to obtain and manipulate monthly and yearly calendars. To
access these methods, you have to import the calendar module:
import calendar For example, to get the calendar for the month
of July, 2016, you can use the following statements:

import calendar
July_cal = calendar.month(2016, 7)
3
print (“Calendar for the month of:”)
print (July_cal)

You’ll get the following:

Current date and time is: Tue Jul 21 09:43:38 2020


Calendar for the month of:
July 2016
MoTu WeTh Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

calendar.firstweekday( )

The function returns the setting for the first weekday of the
week. The default starting day is Monday which is equivalent
to o.

print(calendar.firstweekday())

Output:
0

calendar.isleap(year)

This function evaluates if the given year is a leap year. If yes, it


returns True. If not, it returns False.

print(calendar.isleap(2016))
print(calendar.isleap(2014))

Output:
True
False

calendar.leapdays(y1, y2)

This function returns the sum of leap days within a given range.

print(calendar.leapdays(2010, 2016))

Output:
4
1

calendar.monthrange(year, month)

This function returns two integers. The first integer denotes the
weekday code (o to 6) for the first day of the month in a given
year. The second indicates the number of days in the specified
month (1 to 31).

print(calendar.monthrange(2020, 7))

Output:
(2, 31)

calendar.weekday(year, month, day)

This function provides the weekday code for a specified date.


The parameters are year, month (1 to 12 for the 12 months of
the year), and day (o to 6 from Monday onwards).

print(calendar.weekday(2020,6,30))

Output:
1

You might also like