python get tommorows date dd.mm.yy problem [duplicate] - python

This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
So basically I want to check if a certain string includes tommorows date so i made this date variable (see code).
Now the problem is every month on the last day this is going to be wrong. For example the 30th of september, with the way i did it, its going to say that tommorow will be the 31 of september, wich does not exist however. I need it to say that tommorow is the 1. of october.
Any suggestions? It has to be in the format dd.mm.yy please.
day = str(datetime.datetime.today().day+1)
month = str(datetime.datetime.today().month)
year = str(datetime.datetime.today().year)
date = day + "." + month + "." + year

just add one day to today
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))

relativedelta can also be used
import datetime
from dateutil.relativedelta import relativedelta
tomorrow = datetime.datetime.today() + relativedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))

Related

Python: Add one day to custom date? [duplicate]

This question already has answers here:
Add 1 day to my date in Python [duplicate]
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I'm trying to add one day to custom date(in string). Time format is dd/mm/yyyy
Sample input:
'02/11/2020'
Output:
'03/11/2020'
from datetime import datetime, timedelta
date = '02/11/2020'
old = datetime.strptime(date, "%d/%m/%Y")
new = old + timedelta(days=1)
new_date = new.strftime("%d/%m/%Y")
print(new_date) # '03/11/2020'
You can use the datetime module
from datetime import datetime, timedelta
start = datetime.strptime("%d/%m/%Y", "02/11/2020")
end = start + datetime.timedelta(days = 1)

How to print a date with dashes?

I'm quite lost and I'm in need of trying to format some code so it ends up having dashes in the date. I can get 3, 12, 28 but I can't get 3-12-28. I am a super new beginner so I'm quite lost at the moment.
year = 3
month = 12
day = 28
print(date)
Try
print("{0}-{1}-{2}".format(year,month,day))
You could use datetime to format the result
import datetime
year = 3
month = 12
day = 28
dt = (datetime.date(year, month, day))
print(dt)
the result will be 0003-12-28
if you want more examples of datetime you could take a look at https://docs.python.org/2/library/datetime.html#
As you say you are new to python you can concatenate the strings together.
year = 3
month = 12
day = 28
date = year + "-" + month + "-" + day
print(date)
Alternatively you can use format to set the variables in your required format.
print(f"{year}-{month}-{day}")
Another method is to use datetime if you are using todays date
import datetime
today = datetime.date.today()
print(today)

Find monday of current week in Python [duplicate]

This question already has answers here:
Getting the date of the first day of the week
(2 answers)
Python: give start and end of week data from a given date
(5 answers)
Closed 3 years ago.
I am trying to get the timestamp of monday at 00:00 of the current week in python. I know that for a specific date, the timestamp can be found using
baseTime = int(datetime.datetime.timestamp(datetime.datetime(2020,1,1)))
However, I want my program to automatically find out, based on the date, which date monday of the current week was, and then get the timestamp. That is to say, it would return different dates this week and next week, meaning different timestamps.
I know that the current date can be found using
import datetime
today = datetime.date.today()
Thanks in advance
I am trying to get the timestamp of monday at 00:00 of the current week in python
You could use timedelta method from datetime package.
from datetime import datetime, timedelta
now = datetime.now()
monday = now - timedelta(days = now.weekday())
print(monday)
Output
2020-01-27 08:47:01

python datetime: How to get next period (using aliases such as 'D', 'M') [duplicate]

This question already has answers here:
How to increment datetime by custom months in python without using library [duplicate]
(21 answers)
Closed 9 years ago.
Is there any way to get from a date to the next period? I.e. I am looking for a funaction next that takes
now = datetime.datetime(2013, 11, 15, 0, 0)
to
next(now, 'D') = datetime.datetime(2013, 11, 16, 0, 0) #moving to next day
next(now, 'M') = datetime.datetime(2013,12,31) #moving to next month (day doesn't matter really)
I have tried using Day and MonthEnd from pandas.tseries.offsets but MonthEnd will convert to MonthEnd of the given month, not next month. Is there any simple way to do this?
EDIT: I know this is fairly easy for months. Or days. The problem is, what if I then decide to use business days (Alias 'B')? Or BusinessMonthEnd ('BM')? Surely there should be a method that works for any of these, without having to think about how to implement business days?
How about using dateutil, like this:
from datetime import date
from dateutil.relativedelta import relativedelta
one_day = date.today() + relativedelta (days =+ 1)
one_month = date.today() + relativedelta( months =+ 1 )
You should try the timedelta objects.
Finding the next day is pretty easy, as you just have to add 24 hours:
next=now+datetime.timedelta(hours=24)
The next week is simple as well with days=7, but the next month is a bit tricky because a timedelta with days=31 will sometimes fire you two months later.

How to Print next year from current year in Python

How can I print the next year if the current year is given in python using the simplest code, possibly in one line using datetime module.
Both date and datetime objects have a year attribute, which is a number. Just add 1:
>>> from datetime import date
>>> print date.today().year + 1
2013
If you have the current year in a variable, just add 1 directly, no need to bother with the datetime module:
>>> year = 2012
>>> print year + 1
2013
If you have the date in a string, just select the 4 digits that represent the year and pass it to int:
>>> date = '2012-06-26'
>>> print int(date[:4]) + 1
2013
Year arithmetic is exceedingly simple, make it an integer and just add 1. It doesn't get much simpler than that.
If, however, you are working with a whole date, and you need the same date but one year later, use the components to create a new date object with the year incremented by one:
>>> today = date.today()
>>> print date(today.year + 1, today.month, today.day)
2013-06-26
or you can use the .replace function, which returns a copy with the field you specify changed:
>>> print today.replace(year=today.year + 1)
2013-06-26
Note that this can get a little tricky when today is February 29th in a leap year. The absolute, fail-safe correct way to work this one is thus:
def nextyear(dt):
try:
return dt.replace(year=dt.year+1)
except ValueError:
# February 29th in a leap year
# Add 365 days instead to arrive at March 1st
return dt + timedelta(days=365)
here is another simple way...
import datetime
x = datetime.datetime.now()
print(x.year+1)

Categories