How to get yesterday in python [duplicate] - python

This question already has answers here:
How to subtract a day from a date?
(8 answers)
Closed 8 years ago.
To get now, I can do:
now = datetime.datetime.now()
How would I get 24 hours ago?
now - 24 hrs. ?

Use timedelta:
datetime.datetime.now() - datetime.timedelta(days=1)
http://docs.python.org/2/library/datetime.html#timedelta-objects

you could use:
datetime.date.fromordinal(datetime.date.today().toordinal()-1)
Get yesterday's date in Python, DST-safe
this will help with formatting:
http://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/

Related

keep only year month and day in datetime dataframe pandas [duplicate]

This question already has answers here:
Removing the timestamp from a datetime in pandas dataframe
(2 answers)
Closed 1 year ago.
Could anyone help me to delete the hours and minutes from this datetimes please?
I used this code but it stills returning the same output!
data["expected_Date"]=pd.to_datetime(data["Last_Date"]+ timedelta(days=365*2.9),format = '%Y-%m-%d')
but it returns always this type 2019-01-22 12:00:00 but I want to keep only this 2019-01-22
how can I manage with that please? Thank you!
data["expected_Date"].dt.date

Change the date format to its respective month and year only in python [duplicate]

This question already has answers here:
How to convert a date string to different format [duplicate]
(2 answers)
Closed 1 year ago.
I have a date say - 25-Jan-19
I want to convert it to - Jan19 in python. What date format I'll have to use to get this?
If 25-Jan-19 is a string and will always be in this format, you can try this:-
date = date.split("-")
date = "".join(date[i] for i in range(1,len(date)))

How to get the time of 5 minutes ago in python [duplicate]

This question already has answers here:
How to create a DateTime equal to 15 minutes ago?
(9 answers)
Closed 2 years ago.
I'm using python to get time of 5 minutes ago.
Code:
import datetime
import time
now = datetime.datetime.now()
current_time = now.strftime(f"%Y-%m-%d %H:%M")
print(current_time)
2020-07-27 08:35:00
My question is how to get the time of 5 minutes ago.
something like
current_time-5mins
You may use datetime.timedelta():
datetime.datetime.now() - datetime.timedelta(minutes=5)

How to add hours to a timestamp [duplicate]

This question already has an answer here:
Python datetime add
(1 answer)
Closed 4 years ago.
I have a string variable some_time = "12:30", and I want to add 2 hours to it so the result is "14:30".
I believe by first turning the string into a timeformat,
temp_time = datetime.datetime.strptime(thetime, '%H:%M').time()
so that
>>>print (temp_time)
12:30:00
And then use Pytz can solve it. But I can't seem to find a Pytz command that deals with hh:mm alone, without yy:dd:mm
Any solutions?
If you use a timedelta, you can add two hours like:
Code:
def add_two_hours(time_string):
the_time = dt.datetime.strptime(time_string, '%H:%M')
new_time = the_time + dt.timedelta(hours=2)
return new_time.strftime('%H:%M')
Test Code:
import datetime as dt
print(add_two_hours('12:30'))
print(add_two_hours('23:30'))
Results:
14:30
01:30

Finding the day x years,months from a given time (Python) [duplicate]

This question already has answers here:
python getting weekday from an input date
(2 answers)
Closed 9 years ago.
Have a maths question here which I know to solve using only pen and paper. Takes a while with that approach, mind you. Does anybody know to do this using Python? I've done similar questions involving "dates" but none involving "days". Any of you folk able to figure this one out?
The date on 25/11/1998 is a Wednesday. What is the day on 29/08/2030?
Can anyone at least suggest an algorithm?
Cheers
Use the wonderful datetime module:
>>> import datetime
>>> mydate = datetime.datetime.strptime('29/08/2030', '%d/%M/%Y')
>>> print mydate.strftime('%A')
Tuesday
The algorithm/math is quite simple: There are always 7 days a week. Just calculate how many days between the two days, add it to the weekday of the given day then mod the sum by 7.
<!-- language: python -->
> from datetime import datetime
> given_day = datetime(1998,11,25)
> cal_day = datetime(2030,8,29)
> print cal_day.weekday()
3
> print (given_day.weekday() + (cal_day-given_day).days) % 7
3

Categories