Adding seconds to a in PostgreSQL

The trick is to create a fixed interval and multiply it with the number of seconds in the column: SELECT start_time, expiration_time_seconds, start_time + expiration_time_seconds * interval ‘1 second’ FROM whatever ORDER BY start_time; start_time | expiration_time_seconds | end_time —————————-|————————-|—————————- 2014-08-05 08:23:32.428452 | 172800 | 2014-08-07 08:23:32.428452 2014-08-10 09:49:51.082456 | 3600 | 2014-08-10 10:49:51.082456 2014-08-13 … Read more

How to convert a timedelta object into a datetime object

It doesn’t make sense to convert a timedelta into a datetime, but it does make sense to pick an initial or starting datetime and add or subtract a timedelta from that. >>> import datetime >>> today = datetime.datetime.today() >>> today datetime.datetime(2010, 3, 9, 18, 25, 19, 474362) >>> today + datetime.timedelta(days=1) datetime.datetime(2010, 3, 10, 18, … Read more

Converting string ‘yyyy-mm-dd’ into datetime [duplicate]

Maybe these examples will help you get an idea: from dateutil.relativedelta import relativedelta import datetime date1 = datetime.datetime.strptime(“2015-01-30”, “%Y-%m-%d”).strftime(“%d-%m-%Y”) print(date1) today = datetime.date.today() print(today) addMonths = relativedelta(months=3) future = today + addMonths print(future) If you import datetime it will give you more options in managing date and time variables. In my example above I have … Read more

Python: Difference of 2 datetimes in months [duplicate]

You could use python-dateutil. In [4]: from datetime import datetime In [5]: date1 = datetime.strptime(str(‘2011-08-15 12:00:00’), ‘%Y-%m-%d %H:%M:%S’) In [6]: date2 = datetime.strptime(str(‘2012-02-15’), ‘%Y-%m-%d’) In [7]: from dateutil import relativedelta In [8]: r = relativedelta.relativedelta(date1, date2) In [9]: r Out[9]: relativedelta(months=-5, days=-30, hours=-12)

Python timedelta issue with negative values

If you are using Python 2.7 or higher you can use timedelta.total_seconds() to get a float representation of the timedelta as a positive or negative number of seconds. >>> datetime.timedelta(-1, 86100).total_seconds() -300.0 You should be able to use this to calculate a number of minutes fairly easily. If you are not using Python 2.7 you … Read more

How do you convert a datetime/timestamp from one timezone to another timezone?

If you know your origin timezone and the new timezone that you want to convert it to, it turns out to be very straightforward: Make two pytz.timezone objects, one for the current timezone and one for the new timezone e.g. pytz.timezone(“US/Pacific”). You can find a list of all official timezones in pytz library: import pytz; … Read more

How to add delta to python datetime.time?

datetime.time objects do not support addition with datetime.timedeltas. There is one natural definition though, clock arithmetic. You could compute it like this: import datetime as dt now = dt.datetime.now() delta = dt.timedelta(hours = 12) t = now.time() print(t) # 12:39:11.039864 print((dt.datetime.combine(dt.date(1,1,1),t) + delta).time()) # 00:39:11.039864 dt.datetime.combine(…) lifts the datetime.time t to a datetime.datetime object, the … Read more