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 " 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

Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:10 PM EDT
For Question 1:
I can suggest some minor improvements for readability and clarity:
from datetime import datetime # Parse the input string into a datetime object input_date_str = "18-02-1992" input_date = datetime.strptime(input_date_str, "%d-%m-%Y") # Format the datetime object as a string in the desired format output_date_str = input_date.strftime("%d-%m-%Y") # Print the output date print(f"The input date '{input_date_str}' formatted as '{output_date_str}'") # Output: The input date '18-02-1992' formatted as '18-02-1992'I've added a variable to store the input date string for readability, and also used an f-string to make the print statement more clear
Abhijeet JadhavRank #349 · 7.2K reputationPosted Mar 18, 2023, 6:43 PM EDT
Answer to the Question :1 )
In Python, the default date format is YYYY-MM-DD (Year-Month-Day). That's why when you input "18-02-1992", Python assumes that the date is in DD-MM-YYYY format and converts it to YYYY-MM-DD format.
To get the desired format of "18-02-1992", you need to explicitly format the date using the strftime() method. Here's an example code snippet that shows how to achieve this:
from datetime import datetime # Parse the input string into a datetime object input_date = datetime.strptime("18-02-1992", "%d-%m-%Y") # Format the datetime object as a string in the desired format output_date = input_date.strftime("%d-%m-%Y") # Print the output date print(output_date) # Output: 18-02-1992Abhijeet JadhavRank #349 · 7.2K reputationPosted Mar 18, 2023, 6:47 PM EDT
For Question 2 :
Try this to get the weekday number :
import datetime dt2 = datetime.datetime(2023, 3, 18) # Enclose the strftime() function call within parentheses formatted_date = dt2.strftime('%d-%B-%Y') weekday_number = dt2.weekday() print(formatted_date) # Output: 18-March-2023 print(weekday_number) # Output: 6 (where 0 is Monday and 6 is Sunday)Valerie MeunierRank #1,526 · 1K reputationPosted Mar 19, 2023, 5:59 PM EDT
Thanks again. Again, stupid error ...
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:12 PM EDT
For Question 2:
import datetime # Create a datetime object for March 18th, 2023 dt = datetime.datetime(2023, 3, 18) # Use strftime() method to format the date as a string in a desired format formatted_date = dt.strftime('%d-%B-%Y') # Output format: day-Month-Year # Use weekday() method to get the weekday number (where 0 is Monday and 6 is Sunday) weekday_number = dt.weekday() # Print the formatted date and weekday number print("Formatted date:", formatted_date) # Output: Formatted date: 18-March-2023 print("Weekday number:", weekday_number) # Output: Weekday number: 6Valerie MeunierRank #1,526 · 1K reputationPosted Mar 19, 2023, 6:33 AM EDT
Hi, thanks. The first problem is solved.
For the second, i tried this:
import datetime
dt=datetime. datetime.strptime(input("Input date (dd-mm-jjjj): "),"%d-%m-%Y")
print(f"Weekday of {{dt.strftime('%d-%m-%Y')}} is {dt.weekday}: ")
But this gives the same error, although dt is of type datetime ...
Abhijeet JadhavRank #349 · 7.2K reputationPosted Mar 19, 2023, 3:23 PM EDT
Try this code :
dt = datetime.datetime.strptime(input("Input date (dd-mm-jjjj): "), "%d-%m-%Y") print(f"Weekday of {dt.strftime('%d-%m-%Y')} is {dt.weekday()}: ")