How To Print Date In Python

Photo by Chris Ried on Unsplash

How To Print Date In Python

Introduction

Python is a high-level programming language in computer science. One of the main features of Python is that the written program is easy to read and understand.

Date And The Purpose

Real-time date is very important for front-end development.

In Python, the datetime module provides various classes and methods to work with dates and times. It allows you to manipulate and format dates conveniently and efficiently. The datetime module is part of the standard library, so you don't need to install any additional packages to use it.

How To Use Python Date

To utilize dates in Python, you can leverage the datetime module, which offers a range of classes and methods for date manipulation. Follow these steps :

  1. Start by importing the datetime module:

  2. Create a date object representing a specific date using the datetime.date() constructor

  3. Retrieve the current date using the date.today() method

     import datetime
     date = datetime.date(2023, 6, 1)
     print(date)  # Output: 2023-06-01
     today = datetime.date.today()
     print(today)
    
  4. Access individual components of a date object such as the year, month, and day using their respective attributes

     print(date.year)  # Output: 2023
     print(date.month)  # Output: 6
     print(date.day)  # Output: 1
    
  5. To turn a date object into a string object, use the strftime() method

     str_date = date.strftime("%Y-%m-%d")
     print(str_date)  # Output: 2023-06-01
     print(type(str_date))  #<class 'str'>
    
  6. To turn a string object into a date object, use the strptime() method

    
     date_str = "2023-06-01"
     p_date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
     print(p_date)  # Output: 2023-06-01
     print(type(p_date))  #<class 'datetime.date'>
    

By utilizing the strptime() method, you can convert a string into a datetime object and subsequently extract the date component.

To use weekday in the short version %a (Eg: Wed), in the long version %A (Eg: Wednesday).

To use month in the short version %d (Eg: Dec), in the long version %D (Eg: December).

To use year in the short version(Without century) %y (Eg:18), in the long version %Y (Eg:2018).

How To Change The Timezone

To change the timezone along with the date and time, use a third-party module pytZ

from datetime import datetime
import pytz

local = datetime.now()
print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))


time_NY = pytz.timezone('America/New_York') 
datetime_NY = datetime.now(time_NY)
print("NY:", datetime_NY.strftime("%m/%d/%Y, %H:%M:%S"))

time_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(time_London)
print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))

Here, datetime_NY and datetime_London are datetime objects containing the current date and time of their respective timezone.

Some Uses Of Date In Python

Dates in Python serve multiple purposes across different domains. Here are some applications of dates in Python:

  1. Data Analysis and Visualization: Dates are frequently employed in data analysis and visualization tasks, facilitating trend analysis, time series plotting, data aggregation by date, and generating insightful visual representations.

  2. Event Scheduling and Reminders: Python's date capabilities find extensive use in applications that involve event scheduling and reminders. Dates enable the identification of upcoming events, setting reminders, and triggering notifications based on specific dates and times.

  3. Project Management: Dates are indispensable in project management, aiding in tracking project timelines, setting deadlines, managing milestones, and calculating task durations accurately.

Conclusion

These are merely a handful of examples showcasing the versatile applications of dates in Python. The datetime module's flexibility and extensive functionality make it a powerful tool for handling dates and times in various domains and applications.