Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 84.1k

problem with datetime and weekday

Mar 18 2023 10:16 PM

Hi

I have two questions about dates in python:

Thanks.

1)  when i input e.g. 18-02-1992, i expect to get the same, but instead, i get 1992-02-18. How come and how to get 18-02-1954?

import datetime
dt1 = str(input("Input date (dd-mm-jjjj): "))
dt2=datetime.datetime.strptime(dt1,"%d-%m-%Y").date()
print(dt2) # i get 1992-02-18

2) I want the weekday of the inputted date: the first way works (for 18-02-1992 i get Tuesday), but the second way gives "<buit-in method weekday of datetime.date object? How can i use 'weekday' here?

import datetime
dt1 = str(input("Input date (dd-mm-jjjj): "))
dt2=datetime.datetime.strptime(dt1,"%d-%m-%Y").date()
print(dt2)
print(("De dag van de week voor {} is {}: ").format(dt2,dt2.strftime('%A'))) # this works
print(("De dag van de week voor {} is {}: ").format(dt2.strftime('%d-%B-%Y'),dt2.weekday)) # this doesn't work


Answers (7)