Post

PythonPython

J
James ROBERT

Posted on 16th April 2024|11 views

0
votes

Python Datetime To String

How to convert datetime objects to a string?

Answers
P
Anthony A

Posted on 16th April 2024

The following program converts a datetime object holding the current date and time to various string forms.

 

from datetime import datetime

 

now = datetime.now() # (now)current date and time

 

year = now.strftime("%d")

print("day:", day)

 

month = now.strftime("%m")

print("month:", month)

 

day = now.strftime("%Y")

print("year:", year)

 

Output:

day: 24

month: 6

year: 2020

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos