Skip to main content

Command Palette

Search for a command to run...

WORKING WITH DATETIME FUNCTION IN PYTHON

Updated
4 min read
H

Hi, I’m Happiness Omale, a Developer Relations Advocate, Data Scientist, and Technical Writer passionate about making AI/ML tools feel less intimidating and more useful.

In the past year alone:

  • I led a 3-week community campaign that grew WTM Lagos online engagement by 40%.
  • Wrote and published 15+ technical articles on Python, Data Science, Machine Learning, and AI tools crafted to drive clarity, adoption, and trust.
  • Hosted a Developer vs Designer X AMA that brought in 30+ live listeners and sparked meaningful conversation across our community.

I recently completed a hands-on DevRel training at DxMentorship (Cohort 4), where I deepened my skills in developer advocacy, technical storytelling, product documentation, and community engagement. It gave me the tools and confidence to take on real-world DevRel challenges and the clarity that this is exactly the space I want to grow in.

Whether I’m creating a YouTube tutorial, writing documentation, or hosting a Twitter Space, I thrive at the intersection of tech, storytelling, and community.

I explore emerging AI/ML tools and create demo videos for them on my YouTube channel, "Code & Content with Happiness", helping developers and creators learn faster. I enjoy translating product features into relatable, actionable insights for developers, especially those just getting started.

Right now, I’m looking to join an AI or ML-focused company as a Developer Advocate, somewhere I can drive education, grow communities, and create content that brings real value to users.

Let’s connect if you're building something great and need someone to help developers fall in love with it.

Dates for Python

A date in Python is not a data form of its own, but to work with dates as date objects, we can import a module called datetime. First you have to install it on your computer if you dont't have just simply do "pip install datetime" then you import it. So, in this example I will be showing you how to Import the datetime module and display the current date

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

Date Output

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

   :
   2021-01-20 14:01:32.454684

Year, month, day, hour, minute, second, and microseconds are included in the date.The datetime module has several methods for returning the date object information.

Here are a few examples, you will learn more about them later in this chapter: Example; Return the year and name of weekday

          :
          import datetime
          x = datetime.datetime.now()
          print(x.year)
          print(x.strftime("%A"))

Date Output

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

 :
 2021
 Wednesday

Creating Date Objects

You can use the datetime () class constructor of the datetime module to create a date. For creating a date, the datetime() class includes three parameters: year, month, day. Example; Create a date object

           :
           import datetime
           x = datetime.datetime(2021, 1, 20)
            print(x)

Date Output

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

   :
   2021-01-20 00:00:00

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

The strftime() Method

The datetime object has a method by which date objects can be formatted into readable strings. The method is called strftime() and takes one format parameter to define the format of the string returned. Example; Display the name of the month

  :
  import datetime
  x = datetime.datetime(2021, 1, 1)
  print(x.strftime("%B"))

Date Output

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

  :
 January

A reference of all the legal format codes:

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, without century 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
%z UTC offset +0100
%Z Timezone CST
%j Day number of year 001-366 365
%U Week number of year, Sunday as the first day of week, 00-53 52
%W Week number of year, Monday as the first day of week, 00-53 52
%c Local version of date and time Mon Dec 31 17:41:00 2018
%x Local version of date 12/31/18
%X Local version of time 17:41:00
%% A % character %
%G ISO 8601 year 2018
%u ISO 8601 weekday (1-7) 1
%V ISO 8601 weeknumber (01-53) 01