Check the types and differences of classes that can be used in the datetime module of python with an example.
6 types of classes (data types) are available
datetime.date
: Date (year, month, day)datetime.time
: Time (hours, minutes, seconds, microseconds)datetime.datetime
: date + timedatetime.timedelta
: time differencedatetime.timezone
: Time zone settingdatetime.tzinfo
: Time zone informationCall the datetime module as dt.
Call the datetime module
import datetime as dt
▼ Actual usage example
datetime.date
is written as dt.date
- Datetime.datetime.date will result in an error (name 'datetime' is not defined)
Execution result of dt.date.today ()
datetime.date(2020, 3, 18)
└ datetime.date type
└ (2020, 3, 18) = (year, month, day)
⇒ ** You can get 3 data of year, month and day **. (Attributes: year, month, day)
--dt.time.now (): error --dt.time.today (): Error
type object 'datetime.time' has no attribute 'today'
** ■ How to use ** Specify the time yourself.
This is effective when you do not need date data and want to call or change only time data.
--Specify hours, minute, second, microsecond individually of datetime.time type.
--Call the specified data. (Example: .hour
)
--Change the specified data (Example: .replace (hour = 1)
)
Specified number
Specifying and calling the time (datetime).time)
#Set the time
timeA = dt.time(10, 30, 45, 123456)
#Call the time
timeA.hour #Execution result: 10
timeA.minute #Execution result: 30
timeA.second #Execution result: 45
timeA.microsecond #Execution result: 123456
Change the time (replace)())
#Set the time
timeA = dt.time(10, 30, 45, 123456)
#(1)Change individually
timeA = timeA.replace(hour=5)
timeA
#Execution result
# → datetime.time(5, 30, 45, 123456)
#(2)Change all at once
timeA = timeA.replace(hour=1, minute=2, second=30, microsecond=400)
timeA
#Output result
# → datetime.time(1, 2, 30, 400)
Execution result of dt.datetime.today ()
datetime.datetime(2020, 3, 18, 7, 42, 54, 95450)
└ datetime.datetime type
└ (2020, 3, 18, 7, 42, 54, 95450)
(Year, month, day, hour, minute, second, microsecond)
⇒ ** You can get 7 data of year, month, day, hour, minute, second, and microsecond **. (Attributes: year, month, day, hour, minute, microsecond)
- I also have tzinfo (tz: time zone information) on the back side.
** ▼ tzinfo (tz: time zone information) ** Information is output when the method is executed with now without parentheses.
Display time zone information
dt.datetime.now
<function datetime.now(tz=None)>
Frequently used methods now and today
#.now()
dtNow = dt.datetime.now()
dtNow #datetime.datetime(2020, 3, 18, 21, 28, 13, 409431)
#.today()
dtToday = dt.datetime.today()
dtToday #datetime.datetime(2020, 3, 18, 22, 45, 10, 518281)
Frequently used method combine
#.combine(A, B)
dateA = dt.date(2020,5,6)
timeA = dt.time(10,20,0)
dt.datetime.combine(dateA, timeA)
#Output result
# → datetime.datetime(2020, 5, 6, 10, 30, 0)
and
B = datetime.time type`.If it is different from the specified type such as cobmbine (timeA, dateB), an error will occur.
combine() argument 1 must be datetime.date, not datetime.time
replace
#.replace()
dtNow =dt.datetime.now()
dtNow # datetime.datetime(2020, 3, 18, 22, 10, 15, 517216)
dtNow = dtNow.replace(year=2018)
dtNow = dtNow.replace(hour=5, minute=30, second=30)
dtNow
#Output result
# → datetime.datetime(2018, 3, 18, 5, 30, 30, 474609)
** ■ Output the day of the week **
weekday ()
: 0-6 (Monday 0-Sunday 6)
ʻIsoweekday ()`: 1-6 (Monday 1-Sunday 7)
output | Month | fire | water | wood | Money | soil | Day |
---|---|---|---|---|---|---|---|
weekday() | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
isoweekday() | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Day of the week output
today = dt.datetime.today() #datetime.datetime(2020, 3, 18, 22, 30, 46, 646567)
#.weekday()
today.weekday() #Output: 2 → Wednesday
#.isoweekday()
today.weekday() #Output: 3 → Wednesday
** ■ Output only date or time **
date()
:year、month、day
time()
:hour、minute、second、microsecond
Date / time output
now = dt.datetime.now()
#datetime.datetime(2020, 3, 18, 22, 35, 50, 177279)
#.time()
now.time() #Output: datetime.time(22, 38, 1, 997649)
#.date()
now.date() #datetime.date(2020, 3, 18)
--Timedelta does not add or subtract. -** ★ Specify which item to add / subtract how many ** --dt.timedelta (days = 2): Add 2 days --dttimedelta (hours = -5): subtract 5 hours --Specifiable arguments (7 types. ** * Plural ** --weeks (: multiple of 7days) - days - hours - minutes - seconds - milliseconds - microseconds --Arguments that cannot be specified (** year and month are not possible **) - years - months --Default argument is 0
Unspecified arguments or incorrect arguments (singular, etc.) will result in an error
'years' is an invalid keyword argument for __new__()
** How to use **
(1) Add 2 weeks to January 1, 2020
+ dt.timedelta(weeks=2)
Add with timedelta
newyear = dt.datetime(2020, 1, 1) #Define January 1, 2020 as datetime type
newyear + dt.timedelta(weeks=2)
#Execution result
#datetime.datetime(2020, 1, 15, 0, 0)
#→ January 15, 2020
Subtract with timedelta
newyear = dt.datetime(2020, 1, 1) #Define January 1, 2020 as datetime type
newyear + dt.timedelta(hours=-25)
#Execution result
datetime.datetime(2019, 12, 30, 23, 0)
#→ December 30, 2019 23:00
** ▼ Example ** -No error occurs even if time is added to date type (which does not have time data). ・ The calculated result is displayed. └ Calculated in the hidden part
Handling of data that the mold does not have
newyear2 = dt.date(2020, 1, 1) #datetime.date type (does not have time data)
newyear2 + dt.timedelta(hours=100) #Add 100 hours
#Execution result
#datetime.date(2020, 1, 5)
#→ January 5, 2020
--The types that can be used are datetime.datetime
and datetime.date
.
--datetime.time cannot be used
--The ones to compare need to be the same
--Error occurs with datetime.datetime and datetime.date
--Cannot add (error)
Find the interval between two dates (Case 1)
dateA = dt.date(2020,1,1)
dateB = dt.date(2020,1,9)
dateA - dateB #Output: datetime.timedelta(days=-8)
dateB - dateA #Output: datetime.timedelta(days=8)
Find the interval between two dates(Case 2)
pastA = dt.datetime(2000, 1, 1)
now = dt.datetime.now() #Output: datetime.datetime(2020, 3, 18, 22, 56, 50, 604259)
now - pastA
#Output result
#datetime.timedelta(days=7382, seconds=82358, microseconds=594717)
Addition is an error (* "+" is not supported)
unsupported operand type(s) for +
dt.timezone(dt.timedelta(hours=AAA), 'BBB')
There are two values to set.
--AAA: How much does it deviate from the standard? -+/- 0 ~ 24 * 24 or less --24 is an error --BBB: Name given to the set time zone --Use as an argument when calling
** ▼ Setting method **
① United Kingdom (UCT. Standard: +0 hours) [≒ GMT]
datetime.timezone(datetime.timedelta(0), 'UCT')
② Toronto (EDT.-4)
datetime.timezone(datetime.timedelta(-4), 'EST')
③ Japan (JST. +9)
dt.timezone(dt.timedelta(hours=+9), 'JST')
** ▼ Example of actual usage ** Set the time in Toronto (EDT) and recall the current time.
How to use timezone(Toronto time)
EDT = dt.timezone(dt.timedelta(hours=-4),'EDT')
EDT #Output: datetime.timezone(datetime.timedelta(days=-1, seconds=72000), 'EDT')
dt.datetime.now(EDT)
#Output result
# datetime.datetime(2020, 3, 18, 10, 34, 8, 685415, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), 'EDT'))
** ① UTC: Coordinated Universal Time ** └ Current Coordinated Universal Time └ Calculated from the frequency of cesium atoms
** ② GMT: Greenwich Mean Time ** └ Past standard time └ Calculated from the measurement results of the Greenwich Observatory └ Greenwich is a city just south of London
-Neither is much different (UTC ≒ GMT) --British time --UK is the standard: hours = ± 0
Recommended Posts