Formatting date objects into strings Part - 3

Lesson Details:
June 29, 2020


I: Introduction

The python language is a programming language that was developed in the late 1980s by Guido van Rossum. Guido used to work for a company by the name of Stichting Mathematisch Centrum. This organization was a Netherlands based research facility that had a contract with a security company called CWI or Centrum voor Wiskunde en Informatica. The contract was to develop software for this security company.

In 1989, Guido was tired of working on this project and decided to create a dynamic scripting language that could be used to do interesting things. In the spirit of the time, he named the language after his favorite comedy group, Monty Python. He wrote a simple interpreter and started developing the language over the next few years.

II: Body

A: Formatting date objects into strings part - 3

In our last example we had a date object that did not display the time. We will revisit that here to show that we can format it into a string that includes the time. Use the following code for this example:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- user_input = input("Please enter your birthdate in the form 'mm/dd/yyyy': ") print(user_input) birthdate = datetime.date(int(user_input),int(user_input),int(user_input)) print(birthdate) print("Now displaying the date object in the form 'mmmm yyyy'.") print(strftime("%B %d, %Y")) print("Now displaying the date object in European format.") print(strftime("%d.%m.%Y")) print("Now displaying the date object in American format.") print(strftime("%m/%d/%Y"))

We can see in lines 5 through 7, we use the input function to input the date information from the user. Line 8 shows how to take this information and convert it to an actual date object. Lines 11 through 13 shows how to output this date object in various formats. We can see how to change this format in lines 14 through 16.

A: Learn basic python programming for beginners

We have seen how to create dates and output them in different formats. For this example, we are going to learn how to use other functions related to dates. We are going to use the calendar module that comes with Python by using import calendar . The Calendar module has some useful functions for working with dates. These include month , weekday , isweekday , dayofweek , isherald , weekday , dst , isleap , leapyear , monthrange , yearrange , quarterrange , weekyear . It also provides utilities for creating customized calendars. We will use several of these functions in our example below.

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import calendar user_input = input("Please enter your birthdate in the form 'mm/dd/yyyy': ") print(user_input) birthdate = datetime.date(int(user_input),int(user_input),int(user_input)) print(birthdate) print("Now displaying the date object in the form 'mmmm yyyy'.") print(strftime("%B %d, %Y")) print("Now displaying the date object in European format.") print(strftime("%d.%m.%Y")) print("Now displaying the date object in American format.") print(strftime("%m/%d/%Y")) print("Now displaying the date object in numeric format.") print(birthdate.strftime("%Y-%m-%d")) print("Now displaying the date object in military format.") print(birthdate.strftime("%x")) print("Now displaying the date object in ordinal format.") print(birthdate.strftime("%o")) print("Now displaying the date object in Roman numerals format.") print(calendar.roman_numeral_to_monthname(birthdate)) print("Now displaying the date object in Islamic format.") print(calendar.islamic_to_civil(birthdate)) print("Now displaying the date object in Julian format.") print(calendar.julian_to_datetime(birthdate)) print("Now displaying the date object in Hebrew format.") print(calendar.hebrew_to_civil(birthdate))

We can see line 1 imports the Calendar module and line 2 defines a variable user_input . In lines 3 through 7 we assign this variable a value from the user and store it in a variable called birthdate . In lines 8 through 11 we output this value in various formats including numeric, ordinal, Roman numerals, Islamic and Hebrew formats. In line 12 we show how to get these formatting capabilities from other modules such as strftime .

loader
Course content