timetuple()

timetuple : Details about date and time as elements of a tuple.

Today's details as timetuple

from datetime import date
dt=date.today().timetuple()
print(dt)
Output
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=16, tm_hour=0,
 tm_min=0, tm_sec=0, tm_wday=0, tm_yday=259, tm_isdst=-1)
Getting time details along with date
from datetime import datetime
dt=datetime.now().timetuple()
print(dt)
Output is here
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=17, tm_hour=6, 
tm_min=18, tm_sec=26, tm_wday=1, tm_yday=260, tm_isdst=-1)

Using date object

import datetime
dt=datetime.datetime(2019,12,31)
print(dt.timetuple())
Output
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=31, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=1, tm_yday=365, tm_isdst=-1)
Using datetime
import datetime
# Year, Month, date, Hour, Minute , Second, mircosecond 
dt=datetime.datetime(2019,12,31,23,59,59,345234)
print(dt.timetuple())
Output
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=31, tm_hour=23, 
tm_min=59, tm_sec=59, tm_wday=1, tm_yday=365, tm_isdst=-1)
Details about the elements of timetuple tm_year :Year in YYYY format
tm_month: Month in two digits
tm_mday :Day of the month
th_hour :Hour of the day in 24 hours format
tm_min :Minutes in 2 digits
tm_sec :Seconds in 2 digits
tm_wday :Weekday in number starting with 0 as Sun,1 as Mon ...
tm_yday :Day of the year starting from 1st Jan as 1 and 365 as 31st Dec ( not leap year)
tm_isdst :Daylight Saving Time as flag. It is greater than 0 if DST is in effect. 0 if DST is not in effect and less than 0 if no information is available.

As we get a tuple we can apply all methods of tuple.
Total Number of elements in output is here ( we will use len())
print(len(dt.timetuple()))
output
9
Value of 2nd element i.e date part of the object. ( first element position is 0 )
print(dt.timetuple()[2])
Output is
31
Displaying all values by looping.
for i in dt.timetuple():
    print(i)
Output is here
2019
12
31
23
59
59
1
365
-1
All Date Objects
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer