So, I know how to import time and get lots of little features working, but I don't know how to the time ( in a PM/AM format ) and then do something like ... subtract an hour.
so if the time is:
3:00 PM
the output would be:
2:OO PM
ps. I hope I am asking in the right place and I hope the tags are all I need for people to know this is a python 3 question.
Thank you all!
For the time you would use a
datetime:import pytz from datetime import datetime, timedelta datetime(year=2018, month=11, day=17, hour=15, minute=0, second=0)or probably better, include timezone information (because 3:30 AM minus an hour might be 1:30 AM due to DST):
Then to add an amount of time, you would use
timedelta:So in total:
datetime(year=2018, month=11, day=17, hour=15, minute=0, second=0, tzinfo=pytz.utc) - timedelta(hours=1) # result: datetime.datetime(2018, 11, 17, 14, 0, tzinfo=<UTC>)Note that while for
timedeltaan hour is 60 min is 3600 seconds,a day is not always 24 hours, again because of timezones.If you don't have date or timezone info, there are also
timeandtimedeltaobjects that you can use similarly. But note that timezones are fill of nasty edge cases that you'll be able to handle better with full date+time+locale information.Note that if you want to just store the physical instant something happens, and don't care about communicating it to humans, a timestamp might be easier. This is just the number of seconds since Jan 01 1970 UTC.
It always just increases by 60 every minute, there is no DST, it's the same wherever you are on the world... It's great for computers, but for the same reasons, doesn't really correspond to a user's wall clock.
Also note that none of these things correct for relativity, so don't approach light speed :-)