I want to get the time in Python. With time.ctime()
, there are lots of functions:
I tried:
def write_time():
NUMBER_OF_MIN=40 #my offset
obj=time.gmtime()
print " D", obj.tm_mday, " M",obj.tm_mon, "Y",obj.tm_year,
" time", obj.tm_hour+TIME_OFFSET,":", obj.tm_min-NUMBER_OF_MIN, ":",obj.tm_sec
I want to subtract 40 minutes from the time.
For adding or subtracting Date, we use something called timedelta() function which can be found under the DateTime class. It is used to manipulate Date, and we can perform arithmetic operations on dates like adding or subtracting.
Subtract hours from given timestamp in python using relativedelta. In python, the dateutil module provides a class relativedelta, which represents an interval of time. The relativedelta class has all the attributes to represent a duration i.e. Year, Month, Day, Hours, Minutes, Seconds and Microseconds.
For example, the %H:%M:%S format codes are for hours, minutes, and seconds. To get the difference between two-time, subtract time1 from time2.
Check out the datetime
library, which provides much more flexibility for math using dates.
For example:
import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(minutes=2)
print datetime.datetime.now() - datetime.timedelta(seconds=10)
print datetime.datetime.now() - datetime.timedelta(milliseconds=400)
Prints:
el@dev ~ $ python test.py
2014-11-26 06:47:07.179411
2014-11-26 06:45:07.179538
2014-11-26 06:46:57.179581
2014-11-26 06:47:06.779614
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With