PYTHON : Date Time, Strftime

HOW TO USE PYTHON : date time, Strftime

Date : 30/05/2019

INTRODUCTION

Python date time provides a number of function to deal with date and time .
Before you run the code for datetime, it is important that you import the date time modules.

IMPORT DATE TIME

import date function from date time.

=> from datetime import date

import time function from date time.

=> from datetime import time

import datetime from datetime.

=> from datetime import datetime

from datetime import date
from datetime import time
from datetime import datetime
def main():
    #print today date
    todaydate=datetime.now()
    print (todaydate)
    # Get the current time
    t = datetime.time(datetime.now())
    print "The current time is", t
    
    wd=date.weekday(todaydate)
    
    days= ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]
    print("Today is day number %d" % wd)
    print("which is a " + days[wd])

if __name__== "__main__":
    main()

OUTPUT:

Format Date and Time with Strftime()

As of now we have learned, how to use datetime and date object in Python.

We will advance a step further and learn how to use a formatting function to format Time and Date.

from datetime import datetime
def main():
   
      now= datetime.now() #get the current date and time
        
      print(now.strftime("%c")) 
      print(now.strftime("%x")) 
      print(now.strftime("%X")) 

      #%I/%H - 12/24 Hour, %M - minute, %S - second, %p - local's AM/PM
      print(now.strftime("%I:%M:%S %p")) # 12-Hour:Minute:Second:AM
      print(now.strftime("%H:%M")) # 24-Hour:Minute

if __name__== "__main__":
    main()

%c – local date and time
%x-local’s date
%X- local’s time

OUTPUT:

Thanks for using pheonix solutions.
You find this tutorial helpful? Share with your friends to keep it alive.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top