top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's pictureJosephine Therasa

Exploring Python Date time Module:

Python Datetime Modules supplies classes to work with date and time. These classes provide a number of functions to deal with dates, times, and time intervals. Date and DateTime are an object in Python, so when you manipulate them, you are actually manipulating objects and not strings or timestamps. Date-type functions help us to extract and identify the time-related components like dates, hours, minutes, and days of the week, month, and year. It can extract the day of the week, day of the month, and other date and time formats from strings.



The DateTime module is categorized into 6 main classes –

  • date – An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Its attributes are year, month, and day.

  • time – An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds. Its attributes are hour, minute, second, microsecond, and tzinfo.

  • datetime – It is a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.

  • timedelta – A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.

  • tzinfo – It provides time zone information objects.

In this blog, we are going to check how to create date objects and how to use date functions using datetime modules.

  • Date Objects

To create a date, we can use the datetime() class of the datetime module. The datetime() class requires three parameters to create a date: year, month, and day. The arguments must be in the following range – MINYEAR <= year <= MAXYEAR ,1 <= month <= 12,1 <= day <= number of days in the given month and year


code:








output:

Date  2005-06-15
  • current time

The current date and time can be printed by using the DateTime.now() function .The now() function returns the current local date and time.









output:

current time: 2023-09-21 11:05:45.531209
  • current year, Month, and Date

The extraction of date from the date object can be done by date.today() function.

code:

The code will return the current year , month and date.

Current year: 2023
Current month: 9
Current day: 21
  • strptime() and strftime()

DateTime modules include two methods, strptime() and strftime(), for converting objects from strings to datetime objects and vice versa. strptime() can be used to strings with date and time information and convert them to datetime objects, and strftime() converts datetime objects back into strings.

code:














output:

year: 2023
month: 09
day: 21
time: 12:30:32
date and time: 09/21/2023, 12:30:32

In this blog, we discussed about datetime modules and a few functions that can be used for data analysis and data cleaning. we will explore more about datetime modules in the next blog.


Thanks for Reading.

31 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page