🌐
Programiz
programiz.com › python-programming › datetime › strftime
Python strftime() - datetime to string
from datetime import datetime timestamp = 1528797322 date_time = datetime.fromtimestamp(timestamp) d = date_time.strftime("%c") print("Output 1:", d) d = date_time.strftime("%x") print("Output 2:", d) d = date_time.strftime("%X") print("Output 3:", d) ... Format codes %c, %x and %X are used for locale's appropriate date and time representation. We also recommend you to check Python strptime().
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
Same as date.strftime(). This makes it possible to specify a format string for a date object in formatted string literals and when using str.format(). See also strftime() and strptime() behavior and date.isoformat(). ... >>> import time >>> import datetime as dt >>> today = dt.date.today() >>> today datetime.date(2007, 12, 5) >>> today == dt.date.fromtimestamp(time.time()) True >>> my_birthday = dt.date(today.year, 6, 24) >>> if my_birthday < today: ...
Discussions

Difference between strftime and datetime??
datetime gives you an object that is used in python for time representation and manipulation. >>> from datetime import datetime >>> now = datetime.now() >>> now datetime.datetime(2017, 8, 19, 14, 14, 59, 637824) >>> type(now) >>> now - datetime(2000,1,1,0,0,0) datetime.timedelta(6440, 51299, 637824) It doesn't have any format to speak of, it's just a object with numbers for year, month, day, hours, minutes, seconds, and the subsecond crap. You can do math on it. Given that datetime itself has no format, a function capable of doing so is necessary. Strftime (string format time) is that function. It takes the value of the object and produces a string representation in a given format that uses a bunch of %X flags that stand for different things, so you can use that string for printing nice dates and name things with it. >>> now.strftime('%A') 'Saturday' >>> now.strftime('%B') 'August' >>> now.strftime('%Y-%m-%d') '2017-08-19' https://docs.python.org/3/library/datetime.html there is also strptime (string parse time) that goes in the opposite direction - it is capable of parsing a string containing date and produce the kosher datetime object. More on reddit.com
🌐 r/learnpython
4
4
August 19, 2017
Display the date, like "May 5th", using pythons strftime? - Stack Overflow
You cannot. The time.strftime function and the datetime.datetime.strftime method both (usually) use the platform C library's strftime function, and it (usually) does not offer that format. You would need to use a third-party library, like dateutil. More on stackoverflow.com
🌐 stackoverflow.com
Convert datetime object to a String of date only in Python - Stack Overflow
I see a lot on converting a date string to an datetime object in Python, but I want to go the other way. I've got ... You can use strftime to help you format your date. More on stackoverflow.com
🌐 stackoverflow.com
Whenever: a modern datetime library for Python, written in Rust
As an avid hater of datetime, you have my attention More on reddit.com
🌐 r/Python
125
469
June 22, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strftime-function
Python strftime() function - GeeksforGeeks
July 12, 2025 - In this example, we demonstrate how to extract different parts of the date and time using various format codes like weekday names, month, year, hour, and day of the year. ... from datetime import datetime now = datetime.now() print(now) # Format: Abbreviated weekday, month, 2-digit year f_1 = now.strftime("%a %m %y") print(f_1) # Format: Full weekday, full year f_2 = now.strftime("%A %m %Y") print(f_2) # Format: 12-hour time with AM/PM and seconds f_3 = now.strftime("%I %p %S") print(f_3) # Format: Day of the year f_4 = now.strftime("%j") print(f_4)
🌐
Python
docs.python.org › 3.6 › library › datetime.html
8.1. datetime — Basic date and time types — Python 3.6.15 documentation
date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.
🌐
W3Schools
w3schools.com › python › python_datetime.asp
Python Datetime
The datetime module has many methods to return information about the date object. Here are a few examples, you will learn more about them later in this chapter: ... import datetime x = datetime.datetime.now() print(x.year) print(x.strftime("%A")) Try it Yourself »
🌐
strftime
strftime.org
Python strftime reference cheatsheet
A quick reference for Python's strftime formatting directives.
🌐
PYnative
pynative.com › home › python › python datetime › python datetime format using strftime()
Python DateTime Format using Strftime() – PYnative
May 6, 2022 - from datetime import datetime # Get current time x_time = datetime.now().time() print('Current Time:', x_time) # Represent time in Microseconds (HH:MM:SS.Microsecond) print("Time is:", x_time.strftime("%H:%M:%S.%f"))Code language: Python (python) Run ... As there is no formatting code available for milliseconds, we can only display it using the %S code. However, as milliseconds are 3 decimal places away from seconds, we can display that information by combining %S with %f. ... from datetime import datetime # Current Date and time with milliseconds print(datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) # Output 2021-07-08 08:47:46.851Code language: Python (python) Run
🌐
Reddit
reddit.com › r/learnpython › difference between strftime and datetime??
r/learnpython on Reddit: Difference between strftime and datetime??
August 19, 2017 -

So yesterday I was working with an excel file and I wanted to have a way to include the exact date of that file's creation in the file name. So I used strftime. But I also know that there is a datetime function. Previously, I've never worked with either of these functions, so I can't claim to understand the differences. Which is why I wanted to ask here. What are the differences between these two functions and modules? The time module and the date time module that is.

Top answer
1 of 3
6
datetime gives you an object that is used in python for time representation and manipulation. >>> from datetime import datetime >>> now = datetime.now() >>> now datetime.datetime(2017, 8, 19, 14, 14, 59, 637824) >>> type(now) >>> now - datetime(2000,1,1,0,0,0) datetime.timedelta(6440, 51299, 637824) It doesn't have any format to speak of, it's just a object with numbers for year, month, day, hours, minutes, seconds, and the subsecond crap. You can do math on it. Given that datetime itself has no format, a function capable of doing so is necessary. Strftime (string format time) is that function. It takes the value of the object and produces a string representation in a given format that uses a bunch of %X flags that stand for different things, so you can use that string for printing nice dates and name things with it. >>> now.strftime('%A') 'Saturday' >>> now.strftime('%B') 'August' >>> now.strftime('%Y-%m-%d') '2017-08-19' https://docs.python.org/3/library/datetime.html there is also strptime (string parse time) that goes in the opposite direction - it is capable of parsing a string containing date and produce the kosher datetime object.
2 of 3
2
Time.strftime let's you take all the date/time information and put it in a specific format. It's really useful if you want to output the time, date or both in a specific way. Like I could put out the time in the format: Hour-minute-second time.strftime('%-I-%M-%-S') Or like this Hour:minute:second time.strftime('%-I:%M:%-S') And you can do a lot more with it and add words to it as well time.strftime('Hour%-I:Min%M:Sec%-S') Output: 'Hour11:Min8:Sec5'
Find elsewhere
🌐
W3Schools
w3schools.com › python › gloss_python_date_strftime.asp
Python Date strftime() Method
The datetime object has a method for formatting date objects into readable strings. The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-datetime-strptime
How To Convert a String to a datetime Object in Python | DigitalOcean
December 13, 2024 - Deploy your Python applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app. ... The datetime.strptime() method returns a datetime object that matches the date_string parsed by the format. Both arguments are required and must be strings.
🌐
Codecademy
codecademy.com › docs › python › dates › .strftime()
Python | Dates | .strftime() | Codecademy
January 9, 2025 - The datetime.strftime() method formats time and datetime objects into readable strings on specified format codes. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
freeCodeCamp
freecodecamp.org › news › strftime-in-python
strftime() Python – Datetime Format Tutorial
July 15, 2022 - from datetime import datetime date = datetime.now() string_time = date.strftime("%X") print(string_time) # 00:54:20 · The %X character formats a date string by showing the time representation in hours:minutes:seconds. In this tutorial, we've seen how to format date strings using different characters passed as an argument to the strftime date method. ... There are many other characters for full month names, abbreviated year names, and times. Check out the Python strftime cheatsheet to learn about more characters you can use to represent dates.
🌐
Programiz
programiz.com › python-programming › datetime › strptime
Python strptime() - string to datetime object
Become a certified Python programmer. Try Programiz PRO! ... The strptime() method creates a datetime object from the given string. Note: You cannot create datetime object from every string. The string needs to be in a certain format. from datetime import datetime date_string = "21 June, 2018" ...
Top answer
1 of 15
806

You can use strftime to help you format your date.

E.g.,

import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')

will yield:

'02/23/2012'

More information about formatting see here

2 of 15
325

date and datetime objects (and time as well) support a mini-language to specify output, and there are two ways to access it:

  • direct method call: dt.strftime('format here')
  • format method (python 2.6+): '{:format here}'.format(dt)
  • f-strings (python 3.6+): f'{dt:format here}'

So your example could look like:

  • dt.strftime('The date is %b %d, %Y')
  • 'The date is {:%b %d, %Y}'.format(dt)
  • f'The date is {dt:%b %d, %Y}'

In all three cases the output is:

The date is Feb 23, 2012

For completeness' sake: you can also directly access the attributes of the object, but then you only get the numbers:

'The date is %s/%s/%s' % (dt.month, dt.day, dt.year)
# The date is 02/23/2012

The time taken to learn the mini-language is worth it.


For reference, here are the codes used in the mini-language:

  • %a Weekday as locale’s abbreviated name.
  • %A Weekday as locale’s full name.
  • %w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
  • %d Day of the month as a zero-padded decimal number.
  • %b Month as locale’s abbreviated name.
  • %B Month as locale’s full name.
  • %m Month as a zero-padded decimal number. 01, ..., 12
  • %y Year without century as a zero-padded decimal number. 00, ..., 99
  • %Y Year with century as a decimal number. 1970, 1988, 2001, 2013
  • %H Hour (24-hour clock) as a zero-padded decimal number. 00, ..., 23
  • %I Hour (12-hour clock) as a zero-padded decimal number. 01, ..., 12
  • %p Locale’s equivalent of either AM or PM.
  • %M Minute as a zero-padded decimal number. 00, ..., 59
  • %S Second as a zero-padded decimal number. 00, ..., 59
  • %f Microsecond as a decimal number, zero-padded on the left. 000000, ..., 999999
  • %z UTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030
  • %Z Time zone name (empty if naive), UTC, EST, CST
  • %j Day of the year as a zero-padded decimal number. 001, ..., 366
  • %U Week number of the year (Sunday is the first) as a zero padded decimal number.
  • %W Week number of the year (Monday is first) as a decimal number.
  • %c Locale’s appropriate date and time representation.
  • %x Locale’s appropriate date representation.
  • %X Locale’s appropriate time representation.
  • %% A literal '%' character.
🌐
Mimo
mimo.org › glossary › python › datetime
Python datetime Module: Working with Dates, Times, and Formatting
To work with dates and times in Python, you import the datetime object from the datetime module. The two most common operations are converting strings to datetime objects (strptime) and formatting datetime objects into strings (strftime).
🌐
Python Morsels
pythonmorsels.com › strptime
Find your strptime/strftime format
Paste an example date/time string to see the guessed format or paste a format string to see a formatted example. ... from datetime import datetime example = "Sun, 20 Jul 1969 20:17:40" parsed = datetime.strptime(example, "%a, %d %b %Y %H:%M:%S") formatted_again = f"{parsed:%a, %d %b %Y %H:%M:%S}"
🌐
iO Flood
ioflood.com › blog › strftime-python
Python strftime() | Master Date and Time Formatting
February 7, 2024 - Using an incorrect format code will result in an error, so it’s crucial to get familiar with the different format codes available in Python’s strftime. As you become more familiar with strftime, you can start to use it in more complex ways. For instance, strftime can handle different locales, which allows you to display dates and times according to the conventions of different countries or regions. Let’s look at an example where we format the current date in a specific locale: import locale from datetime import datetime # Set the locale to French locale.setlocale(locale.LC_TIME, 'fr_FR') current_time = datetime.now() formatted_time = current_time.strftime('%A %d %B %Y') print(formatted_time) # Output: # 'mardi 01 février 2022'