Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Handle Time in Python: Strptime ()() Function in Python

Learn the key facts about Handle Time in Python: Strptime ()() Function in Python, with clear context, practical guidance, and useful takeaways.

Table of Contents

This updated guide examines Handle Time in Python: Strptime ()() Function in Python and organizes the essential facts, background, and practical takeaways in clear American English.

The strptime () function in Python is used to create the datetime object from a given string. However, not any string can be passed into the function, the string here must satisfy a certain format to return the result.

Handle Time in Python: Strptime ()() Function in Python — contextual image 1
strptime () in Python

Example 1: Convert string to datetime object

from datetime import datetime date_string = "11 July, 2019" print("date_string =", date_string) date_object = datetime.strptime(date_string, "%d %B, %Y") print("date_object =", date_object)

Run the program, the result returned:

date_string = 11 July, 2019 date_object = 2019-07-11 00:00:00

How does strptime () work?

Strptime () has two parameters:

  • The string will be converted to datetime.
  • Codeformat .

Based on the string and format code passed, the method returns its corresponding datetime object.

In the above example:

Handle Time in Python: Strptime ()() Function in Python — contextual image 2
  • %d: Representing the day of the month. For example: 01, 02,., 31.
  • %B: Full month name. Example: January, February.
  • %Y: Four-digit year. Examples: 2018, 2019.

Example 2: Convert string to datetime object

from datetime import datetime dt_string = "11/07/2018 09:15:32" # Định dạng ở dạng dd/mm/yyyy dt_object1 = datetime.strptime(dt_string, "%d/%m/%Y %H:%M:%S") print("dt_object1 =", dt_object1) # Định dạng ở dạng mm/dd/yyyy dt_object2 = datetime.strptime(dt_string, "%m/%d/%Y %H:%M:%S") print("dt_object2 =", dt_object2)

Run the program, the result returned:

dt_object1 = 2018-07-11 09:15:32 dt_object2 = 2018-11-07 09:15:32

List of code formats

The table below shows all the formatting codes that you can pass to the strptime () method.

SignMeaningFor example %aAbbreviated name of the week Sun, Mon.%AThe day of the week is written in full Sunday, Monday.%wDay of the week, numeric values 0, 1,., 6%dDate of the month, numeric value form (value 0 as a buffer before the 1-digit date) 01, 02,., 31%-dDay of the month, value form 1, 2,., 30%bAbbreviated month name Jan, Feb,., Dec%BFull month name, January, February.%mMonth of the year, numeric value (value 0 as a buffer before the month has 1 digits) 01, 02,., 12%-mMonth of the year, value form 1, 2,., 12%y2-digit year value (with value 0 as buffer before the year has 1 digit) 00, 01,., 99%-yYear 2 digit values 0, 1,., 99%YFull year value 2013, 2019.%HHourly system 24 language (value 0 as a buffer before 1 digit) 00, 01,., 23%-H24 hour system time, format values of 0, 1,., 23%I12 hour system time, numeric value (value 0 as a buffer with 1 digit before) 01, 02,., 12%-I12 hour system hours 1, 2,., 12%pLocal time is AM or PM. AM, PM%MMinutes, numeric values (value 0 as 1-minute pre-minute buffers) 00, 01,., 59%-MMinutes, numeric values 0, 1,., 59%SSeconds, numeric values (value 0 as a buffer before the 1-digit second) 00, 01,., 59%-SSeconds, numeric values 0, 1,., 59%fMicro seconds, numeric value (value 0 as a 1-second pre-pad buffer) 000000 - 999999%zUTC compensation time in + HHMM or -HHMM format.%ZTime zone name%jDate of the year, numeric value (value 0, 00 as a buffer before the date has 1 and 2 digits) 001, 002,., 366%-jYear of the year, form values of 1, 2,., 366%UNumber of weeks in the year (Sunday is the first day of the week). All days in the new year before the first Sunday are considered as during the week of 00, 01,., 53%WNumber of weeks in the year (Monday is the first day of the week). All days in the new year before the first Monday are considered in week 0. 00, 01,., 53%cReturns the date and time Sep 30 30:06:05 2013%xReturns on 09 / 30/13%XReturns 7:06:05%%'%' character literally. %

ValueError results returned from strptime ()

If the two string parameters and the format code passed into strptime () do not match, you will get the ValueError result .

from datetime import datetime date_string = "11/07/2018" date_object = datetime.strptime(date_string, "%d %m %Y") print("date_object =", date_object)

Results returned:

ValueError: time data '11/07/2018' does not match format '%d %m %Y'

Previous lesson: strftime () function in Python

Next lesson: The current date and time in Python

FAQ

What is Handle Time in Python: Strptime ()() Function in Python about?

It provides a structured overview of handle time in python, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.