Understanding timedelta

Because timedelta is defined like: class datetime.timedelta([days,] [seconds,] [microseconds,] [milliseconds,] [minutes,] [hours,] [weeks]) All arguments are optional and default to 0. You can easily say “Three days and four milliseconds” with optional arguments that way. >>> datetime.timedelta(days=3, milliseconds=4) datetime.timedelta(3, 0, 4000) >>> datetime.timedelta(3, 0, 0, 4) #no need for that. datetime.timedelta(3, 0, 4000) And for … Read more

What is the difference between “datetime.timedelta” and “dateutil.relativedelta.relativedelta” when working only with days?

dateutil is an extension package to the python standard datetime module. As you say, it provides extra functionality, such as timedeltas that are expressed in units larger than a day. This is useful if you have to ask questions such as how many months can I save before my girlfriend’s birthday comes up, or what’s … Read more

How to construct a timedelta object from a simple string

To me the most elegant solution, without having to resort to external libraries such as dateutil or manually parsing the input, is to use datetime’s powerful strptime string parsing method. from datetime import datetime, timedelta # we specify the input and the format… t = datetime.strptime(“05:20:25″,”%H:%M:%S”) # …and use datetime’s hour, min and sec properties … Read more

Convert timedelta to years?

You need more than a timedelta to tell how many years have passed; you also need to know the beginning (or ending) date. (It’s a leap year thing.) Your best bet is to use the dateutil.relativedelta object, but that’s a 3rd party module. If you want to know the datetime that was n years from … Read more

How to add hours to current time in python

from datetime import datetime, timedelta nine_hours_from_now = datetime.now() + timedelta(hours=9) #datetime.datetime(2012, 12, 3, 23, 24, 31, 774118) And then use string formatting to get the relevant pieces: >>> ‘{:%H:%M:%S}’.format(nine_hours_from_now) ’23:24:31′ If you’re only formatting the datetime then you can use: >>> format(nine_hours_from_now, ‘%H:%M:%S’) ’23:24:31′ Or, as @eumiro has pointed out in comments – strftime

Convert a timedelta to days, hours and minutes

If you have a datetime.timedelta value td, td.days already gives you the “days” you want. timedelta values keep fraction-of-day as seconds (not directly hours or minutes) so you’ll indeed have to perform “nauseatingly simple mathematics”, e.g.: def days_hours_minutes(td): return td.days, td.seconds//3600, (td.seconds//60)%60

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)