‘strftime’ Python
strftime
is a method in Python used for formatting date and time objects. It stands for “string format time” and is part of the datetime
module, which provides various functionalities for manipulating dates and times. strftime
allows you to convert a datetime
object into a string formatted according to your specifications.
Here’s a basic guide on how to use strftime
:
Import the
datetime
Module: First, you need to import thedatetime
class from thedatetime
module.pythonfrom datetime import datetime
Create a
datetime
Object: You can create adatetime
object representing the current time, or you can specify a particular date and time.Current time:
pythonnow = datetime.now()
Specific date and time:
pythonsome_date = datetime(2024, 1, 11, 8, 30)
Use
strftime
to Format the Date and Time: Thestrftime
method takes one argument: a format string in which various directives represent the parts of the date and time.Example:
pythonformatted_date = now.strftime("%Y-%m-%d %H:%M:%S") print(formatted_date)
In this example,
%Y
is replaced by the full year,%m
by the month as a zero-padded decimal number,%d
by the day of the month as a zero-padded decimal number,%H
by the hour (24-hour clock) as a zero-padded decimal number,%M
by the minute as a zero-padded decimal number, and%S
by the second as a zero-padded decimal number.
Here are some common directives used in strftime
:
%Y
: Year with century as a decimal number.%m
: Month as a zero-padded decimal number.%d
: Day of the month as a zero-padded decimal number.%H
: Hour (24-hour clock) as a zero-padded decimal number.%M
: Minute as a zero-padded decimal number.%S
: Second as a zero-padded decimal number.%A
: Weekday as locale’s full name (e.g., Sunday, Monday).%B
: Month as locale’s full name (e.g., January, February).
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks