logo-MindMajix
PostPythonPython
J
James ROBERT

Posted on 23rd June 2024|12 views

0
votes

Python Datetime To String

How to convert datetime objects to a string?

1 answers
Answers
P
Anthony A

Posted on 23rd June 2024| views

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
Name
Course *
Email *
Phone Number