154

For example, on July 5, 2010, I would like to calculate the string

 July 5, 2010

How should this be done?

0

6 Answers 6

266

You can use the datetime module for working with dates and times in Python. The strftime method allows you to produce string representation of dates and times with a format you specify.

>>> import datetime
>>> datetime.date.today().strftime("%B %d, %Y")
'July 23, 2010'
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
'10:36AM on July 23, 2010'
Sign up to request clarification or add additional context in comments.

1 Comment

I found this to be a helpful reference when trying to make different custom formats: strftime.org
53
#python3

import datetime
print(
    '1: test-{date:%Y-%m-%d_%H:%M:%S}.txt'.format( date=datetime.datetime.now() )
    )

d = datetime.datetime.now()
print( "2a: {:%B %d, %Y}".format(d))

# see the f" to tell python this is a f string, no .format
print(f"2b: {d:%B %d, %Y}")

print(f"3: Today is {datetime.datetime.now():%Y-%m-%d} yay")

#4: to make the time timezone-aware pass timezone to .now()
tz = datetime.timezone.utc
ft = "%Y-%m-%dT%H:%M:%S%z"
t = datetime.datetime.now(tz=tz).strftime(ft)
print(f"4: timezone-aware time: {t}")

1: test-2018-02-14_16:40:52.txt

2a: March 04, 2018

2b: March 04, 2018

3: Today is 2018-11-11 yay

4: timezone-aware time: 2022-05-05T09:04:24+0000


Description:

Using the new string format to inject value into a string at placeholder {}, value is the current time.

Then rather than just displaying the raw value as {}, use formatting to obtain the correct date format.

https://docs.python.org/3/library/string.html#formatexamples

https://docs.python.org/3/library/datetime.html

3 Comments

what does f mean in print(f"3?
@lei-yang here is a explanation: realpython.com/python-f-strings it marks the string as f string, and python then looks for { } with code/variables in it, and places the content in the string. It is the latest python3.6 string formatting addition
The bottom of this page https://docs.python.org/3/library/datetime.html lists the placeholders like %B and what they represent.
26
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%B %d, %Y")
'July 23, 2010'

Comments

13

If you don't care about formatting and you just need some quick date, you can use this:

import time
print(time.ctime())

Comments

4

With time module:

import time
time.strftime("%B %d, %Y")
>>> 'July 23, 2010'
time.strftime("%I:%M%p on %B %d, %Y")
>>> '10:36AM on July 23, 2010'

For more formats: www.tutorialspoint.com

Comments

2

Simple

# 15-02-2023
datetime.datetime.now().strftime("%d-%m-%Y")

and for this question use "July 5, 2010"

# July 5, 2010
datetime.datetime.now().strftime("%B %d, %Y")

1 Comment

That's not the format requested in the question, is it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.