8 Antworten
+ 9
I am not sure if this is what you like to get:
from datetime import datetime
my_date = datetime.strptime('2017-05-04',"%Y-%m-%d")
print(my_date)
#2017-05-04 00:00:00
my_date = my_date.replace(year=2018)
print(my_date)
#2018-06-01 23:59:59
my_date2 = datetime.today()
print(my_date2)
my_date2 = my_date2.replace(year=2017)
print(my_date2)
+ 8
still not sure if i understand it right now. So i assume you want to say:
Monday, 2019-01-02
Tuesday, 2019-01-03
...
Tuesday, 2019-12-31
???
+ 7
Here some code you can try. Unfortunately "dateutil" module is not availlable on Solo playground. Try it on the system where you want to use it:
from datetime import datetime as dt_dt
from dateutil.relativedelta import relativedelta
def is_leape(my_year):
return my_year % 4 == 0 and (my_year % 400 == 0 or my_year % 100 != 0)
my_date = dt_dt.strptime('2020-01-01',"%Y-%m-%d") # drfine date to use
year_days = 366 if is_leape(my_date.year) else 365
for i in range(0, year_days):
print(my_date.strftime("%A, %Y-%m-%d"))
my_date += relativedelta(days=1)
if you run in trouble and dateutil is not running on your system please come back, so that we can look for an other way to do it.
+ 6
so - here is a version that does not need relativdelta. It should run on playground but you may run in "time limit"
https://code.sololearn.com/c1txQDxtf30c/?ref=app
+ 6
Final version, now without separate check for leap year. But working with leap years.
from datetime import date, timedelta
str_date = date(2020, 1, 1)
end_date = date(2020, 12, 31)
for i in range(((end_date - str_date).days) + 1):
print((str_date + timedelta(i)).strftime("%A, %Y-%m-%d"))
+ 4
Thank you.
What I intend to do is to create a code having days in a week with a date for each day for a whole year.
# For example, the below code contains days in a week.
weekdays = ("Monday:", "Tuesday:", "Wednesday:", "Thursday:", "Friday:", "Saturday:", "Sunday:")
for i in weekdays:
print(input(i ))
# How do I give each day a date spanning, say, 2019?
+ 2
Yes, you are right
+ 1
Thank you so much. It worked.