0

using python 3.9

I want to create a program that instead of printing 2/28/2021, it prints Sunday, 2, Feb, 2021.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Have you read the `strftime()` [documentation](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior)? – martineau Feb 28 '21 at 12:00
  • Does this answer your question? [How do I turn a python datetime into a string, with readable format date?](https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date) – FObersteiner Feb 28 '21 at 15:00

3 Answers3

0

You can do it using strptime() to achieve a human-readable string.

import datetime
a = datetime.datetime.now()
print(a.strftime("%a, %d, %B, %Y")) # Sun, 28, February, 2021
print(a.strftime("%A, %d, %B, %Y")) # Sunday, 28, February, 2021

import locale
locale.setlocale(locale.LC_TIME, "sv_SE") # swedish locale
print(a.strftime("%a, %d, %B, %Y")) # sön, 28, februari, 2021
Axisnix
  • 2,822
  • 5
  • 19
  • 41
0
import datetime

x = datetime.datetime.now().strftime('%A, %-d, %b, %Y')
print(x)  # output: Sunday, 28, Feb, 2021

x = datetime.datetime.now().strftime('%-m/%d/%Y')
print(x)  # output: 2/28/2021
dopstar
  • 1,478
  • 10
  • 20
-1

You can do that with the datetime method strftime.

from datetime import datetime
print(datetime.now().strftime("%A %d. %B %Y"))