Difference between strftime and datetime??
Display the date, like "May 5th", using pythons strftime? - Stack Overflow
Convert datetime object to a String of date only in Python - Stack Overflow
Whenever: a modern datetime library for Python, written in Rust
Videos
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.
strftime doesn't allow you to format a date with a suffix.
Here's a way to get the correct suffix:
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
found here
Update:
Combining a more compact solution based on Jochen's comment with gsteff's answer:
from datetime import datetime as dt
def suffix(d):
return {1:'st',2:'nd',3:'rd'}.get(d%20, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
print(custom_strftime('%B {S}, %Y', dt.now()))
Gives:
May 5th, 2011
This seems to add the appropriate suffix, and remove the ugly leading zeroes in the day number:
#!/usr/bin/python
import time
day_endings = {
1: 'st',
2: 'nd',
3: 'rd',
21: 'st',
22: 'nd',
23: 'rd',
31: 'st'
}
def custom_strftime(format, t):
return time.strftime(format, t).replace('{TH}', str(t[2]) + day_endings.get(t[2], 'th'))
print(custom_strftime('%B {TH}, %Y', time.localtime()))
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
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:
%aWeekday as locale’s abbreviated name.%AWeekday as locale’s full name.%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.%dDay of the month as a zero-padded decimal number.%bMonth as locale’s abbreviated name.%BMonth as locale’s full name.%mMonth as a zero-padded decimal number. 01, ..., 12%yYear without century as a zero-padded decimal number. 00, ..., 99%YYear with century as a decimal number. 1970, 1988, 2001, 2013%HHour (24-hour clock) as a zero-padded decimal number. 00, ..., 23%IHour (12-hour clock) as a zero-padded decimal number. 01, ..., 12%pLocale’s equivalent of either AM or PM.%MMinute as a zero-padded decimal number. 00, ..., 59%SSecond as a zero-padded decimal number. 00, ..., 59%fMicrosecond as a decimal number, zero-padded on the left. 000000, ..., 999999%zUTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030%ZTime zone name (empty if naive), UTC, EST, CST%jDay of the year as a zero-padded decimal number. 001, ..., 366%UWeek number of the year (Sunday is the first) as a zero padded decimal number.%WWeek number of the year (Monday is first) as a decimal number.%cLocale’s appropriate date and time representation.%xLocale’s appropriate date representation.%XLocale’s appropriate time representation.%%A literal '%' character.