Python datetime How to calculate dates and convert strings strftime, strptime [Definitive Edition]

Python datetime How to calculate date and convert strings strftime, strptime </ strong>

2019/08/02 Last updated

Here, get / calculate date by Python standard datetime timedelta () </ strong>, convert to string strftime () </ strong>, convert from string strptime () </ strong> I will explain how to do it.

Click here to build a Python environment on Mac. Install Python 3 on Mac and build environment [Definitive Edition]

Get each value of the current time and date and time

Get the current time and datetime values (year, month, day, hour, minute, second, day of the week) from datetime.

import datetime


"""
Get the current date and time from datetime
"""
now = datetime.datetime.now() # => datetime.datetime(2019, 8, 2, 1, 57, 9, 99551)


"""
Get each value from datetime
Year,Month,Day,Time,Minutes,Seconds
"""
now.year # => 2019
now.month # => 8
now.day # => 2
now.hour # => 1
now.minute # => 57
now.second # => 9


"""
Get the day of the week from datetime
Correspondence between weekday and day of the week
    0:Month, 1:fire, 2:water, 3:wood, 4:Money, 5:soil, 6:Day
"""
now.weekday() # => 4

In addition to year, month, day, hour, minute, second, you can also get microsecond.

Datetime date calculation (addition, subtraction)

Use the timedelta () </ strong> method to calculate the addition and subtraction of dates with datetime. Calculations that change the date are also possible.

import datetime


"""
datetime date calculation
     weeks, days, hours, minutes, seconds
"""
now = datetime.datetime.now() # => datetime.datetime(2019, 8, 2, 1, 59, 33, 338054)

#1 week addition
now + datetime.timedelta(weeks=1) # => datetime.datetime(2019, 8, 9, 1, 59, 33, 338054)
#10 days subtraction
now - datetime.timedelta(days=10) # => datetime.datetime(2019, 7, 23, 1, 59, 33, 338054)
#10 hours addition
now + datetime.timedelta(hours=10) # => datetime.datetime(2019, 8, 2, 11, 59, 33, 338054)
#10 minutes subtraction
now - datetime.timedelta(minutes=10) # => datetime.datetime(2019, 8, 2, 1, 49, 33, 338054)
#10 seconds addition
now + datetime.timedelta(seconds=10) # => datetime.datetime(2019, 8, 2, 1, 59, 43, 338054)

#The 7th,1 hour,10 minutes addition
now + datetime.timedelta(days=7, hours=1, seconds=10) # => datetime.datetime(2019, 8, 9, 2, 59, 43, 338054)

In addition to weeks, days, hours, minutes, seconds, milliseconds and microseconds can also be calculated.

Convert datetime, date to string

Use the strftime () </ strong> method to convert from datetime, date to a string.

import datetime


"""
Convert from datetime to string
"""
now = datetime.datetime.now()


#Example 1
now.strftime('%Y-%m-%d %H:%M:%S') # => '2019-08-02 02:20:43'
#Example 2
now.strftime('%Y year%m month%d day') # => '08/02/2019'
#Example 3
now.strftime('%A, %B %d, %Y') # => 'Friday, August 02, 2019'

Convert string to datetime, date

Use the strptime () </ strong> method to convert a string to datetime, date.

import datetime


"""
Convert from string to datetime
"""
#Example 1
string_date_1 = '2019/08/02'
datetime.datetime.strptime(string_date_1, '%Y/%m/%d') # => datetime.datetime(2019, 8, 2, 0, 0)
#Example 2
string_date_2 = '2019/08/02 2:03:07'
datetime.datetime.strptime(string_date_2, '%Y/%m/%d %H:%M:%S') # => datetime.datetime(2019, 8, 2, 2, 3, 7)
#Example 3
string_date_3 = '2019-08-02 12:06:19'
datetime.datetime.strptime(string_date_3, '%Y-%m-%d %H:%M:%S') # => datetime.datetime(2019, 8, 2, 12, 6, 19)


"""
string,Convert from datetime to date
"""
string_date = '2019/08/02 12:06:19'
# string to datetime
dt = datetime.datetime.strptime(string_date, '%Y/%m/%d %H:%M:%S') # => datetime.datetime(2019, 8, 2, 12, 6, 19)
# datetime to date
datetime.date(dt.year, dt.month, dt.day) # => datetime.date(2019, 8, 2)

Summary

It was an explanation of how to get and calculate the date and time by datetime, and how to convert from a character string. The datetime type is convenient because it can be used as it is for the axis when creating a graph with matplotlib or plotly.

Official documentation on datetime

** Like ** will motivate you, so I'd be happy if you could press it.

Recommended Posts