datetime
datetime
datetime is used for the time value in Python scripts.
Before using datetime, enter 'import datetime' to use datetime.
import datetime
Methods
Class |
Description |
Represent Gregorian Calendar. |
|
Represent time as an hour, minute, second, microsecond, and time zone. |
|
The combination of date class and time class. Represents the information of year, month, day, hour, minute, second, microsecond, and time zone. |
|
Represent the variation between two dates or time. |
datetime.date class represents Gregorian Calendar. ㆍ Constructor : datetime.date(Year, Month, Day) ㆍ Year : 1~9999 ㆍ Month : 1~12 ㆍ Day : 1~ the last day of months. Example) import datetime DT = datetime.date(2014, 1, 1) print(DT) >> 2014-01-01 #If an invalid value entered, it occurs an error. #datetime.date must enter all parameters. If not, it occurs an error.
|
||||||||||||
The property of datetime.date. |
||||||||||||
|
||||||||||||
Example) import datetime DT = datetime.date(2014, 1, 1) print(DT.year) >> 2014 print(DT.month) >> 1 print(DT.day) >> 1
DDay = datetime.date.today() print(DDay) >> 2014-02-04
#The Integer returned by weekday() are 0(Monday), 1(Tuesday), 2(Wednesday), 3(Thursday), 4(Friday), 5(Saturday), 6(Sunday). Week = datetime.date.today() print(Week.weekday()) >> 1
|
||||||||||||
datetime.time class represents the time of an hour, minute, and second. ㆍ Constructor : datetime.time(Hour, Minute, Second) ㆍ Hour : 0~23 Hour ㆍ Minute : 0~59 Minute ㆍ Second : 0~59 Second Example) import datetime a = datetime.time(23) print(a) >> 23:00:00
a = datetime.time(23,59) print(a) >> 23:59:00
a = datetime.time(23,59,59) print(a) >> 23:59:59
#If an invalid value entered, it occurs an error.
|
||||||||
The property of datetime.date |
||||||||
|
||||||||
Example) import datetime
tm = datetime.time(10, 15, 20) print( tm.hour ) >> 10 print( tm.minute ) >> 15 print( tm.second ) >> 20
|
||||||||
datetime.datetime class represents a year, month, day, hour, minute, second. ㆍ Constructor : datetime.datetime(Year, Month, Day, Hour, Minute, Second) ㆍ Year : 1~9999 Year ㆍ Month : 1~12 Month ㆍ Day : 1~the last day of months ㆍ Hour : 0~23 ㆍ Minute : 0~59 ㆍ Second : 0~59 ㆍ Create the datetime object by taking each parameter as an Integer number. You can't omit the year, month, day. If hour, minute, second is omitted, reset to zero. Example) import datetime a = datetime.datetime(2014, 1, 1) print(a) >> 2014-01-01 00:00:00
a = datetime.time(2014, 12, 31, 23) print(a) >> 2014-12-31 23:00:00 #If an invalid value entered, it occurs an error.
|
||||||||||||||||||||||
The property of datetime.date. |
||||||||||||||||||||||
|
||||||||||||||||||||||
Example) import datetime
tm = datetime.datetime(2014, 12, 31, 10, 15, 20) print( tm.year ) >> 2014 print( tm.month ) >> 12 print( tm.day ) >> 31 print( tm.hour ) >> 10 print( tm.minute ) >> 15 print( tm.second ) >> 20
tm = datetime.datetime.today(); print( tm ) >> 2014-02-26 16:53:44.095000 #today() obtains by millisecond, it obtains the local time(Desktop time).
tm = datetime.datetime(2014, 12, 31, 10, 15, 20) print(tm.weekday()) >> 2 #It is the same as weekday in date class.
tm = datetime.datetime(2014, 12, 31, 10, 15, 20) dt = tm.date(); print(dt) >> 2014-12-31
dt = tm.time(); print(dt) >> 10
|
||||||||||||||||||||||
datetime.timedelta class represents the variation between two dates or time. If the parameter is a positive number, it represents forward from the current point, If it is a negative number, it represents backward from the current point. ㆍ Constructor : datetime.timedelta(weeks, days, hours, minutes, seconds) |
||||||||||||||||
Parameters of datetime.timedelta. |
||||||||||||||||
|
||||||||||||||||
datetime.timedelta is used to change the time of time information object made by datetime class. Example) import datetime tm = datetime.datetime(2014, 12, 31, 10, 15, 20) dt = tm.date(); print(tm) >> 2014-12-31 10:15:20
# Enter weeks 3 to the timedelta class, and save the information of 3weeks. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm - datetime.timedelta(weeks=3) Subtract 3weeks from the current time. print(tm) >> 2014-12-10 10:15:20
# Enter weeks -3 to the timedelta class, and save the information of -3weeks. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm + datetime.timedelta(weeks=-3) Add -3weeks to the current time. print(tm) >> 2014-12-10 10:15:20 # Enter days 5 to the timedelta class, and save the information of 5days. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm + datetime.timedelta(days=5) Add 5days to the current time. print(tm) >> 2015-01-05 10:15:20
# Enter hours 1 to the timedelta class, and save the information of an hour. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm + datetime.timedelta(hours=1) Add an hour to the current time. print(tm) >> 2014-12-31 11:15:20
# Enter minute 11 to the timedelta class, and save the information of 11minutes. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm - datetime.timedelta(minutes=11) Subtract 11minutes from the current time. print(tm) >> 2014-12-31 10:04:20
# Enter seconds 29 to the timedelta class, and save the information of 29seconds. tm = datetime.datetime(2014, 12, 31, 10, 15, 20) tm = tm + datetime.timedelta(seconds=29) Add 29seconds to the current time. print(tm) >> 2014-12-31 10:15:49
|
||||||||||||||||