Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python strptime

by suresh

The Python strptime function is available in both the datetime and time module. This Python strptime function helps you to parse the datetime, date, or time in string representation.

This Python strptime function uses some directives to parse the string date. Remember, these are the same directives that we used in the strftime function to format dates and times. The syntax of this strptime in Python

datetime.strptime(string_value, format) # format using directives

Let me see the supporting Python strptime directives

  • %a – Short Version of Weekday (Wed)
  • %A – Full Version of Weekday (Wednesday)
  • %b – Short Version of Month Name (Jan)
  • %B – Full Version of Month Name (January)
  • %c – Local Version of Date and Time
  • %d  – Day Number of a Month 01 to 31
  • %f – Microseconds 000000 to 999999
  • %H – Hours 00 to 23
  • %I – Hours 00 to 12
  • %j – Day Number of the Year 001 to 366
  • %m – Month as a Number 01 to 12
  • %M – Minutes from 00 to 59
  • %p – AM or PM
  • %S – Seconds from 00 to 59
  • %U – Week Number of the Year from 00 to 53, where First Day of the Week = Sunday
  • %w – Weekday as a Number from 0 to 6, Where 0 = Sunday
  • %W – Week Number of the Year from 00 to 53, where First Day of the Week = Monday
  • %x – Local Version of Date (05/23/19)
  • %X – Local Version of Time (10:45:32)
  • %y – Short Version of Year (19)
  • %Y – Full Version of the Year (2019)
  • %z – UTC Offset
  • %Z – Timezone

Python strptime Examples

The strptime function converts the given string date in Day/Month/Year format to the local version of DateTime.

# Python strptime Example
 
from datetime import datetime
 
datetime_value = datetime.strptime('30/12/18', '%d/%m/%y')
 
print(datetime_value)
Python strptime 1

This python strptime accepts the date in DayNumber MonthName and FullYear format and returns the Date and Time.

from datetime import datetime
 
dt_string = '31 December 2018'
datetime_value = datetime.strptime(dt_string, '%d %B %Y')
print(datetime_value)
 
dt_str = '31 December 17'
dt_value = datetime.strptime(dt_str, '%d %B %y')
print(dt_value)
Python strptime 2

Using the strptime function to convert the datetime string.

from datetime import datetime
 
dt_string = '31/12/18 23:59:58'
datetime_value = datetime.strptime(dt_string, '%d/%m/%y %H:%M:%S')
print(datetime_value)
 
dt_string = '10-12-2017 19:12:58'
dt_value = datetime.strptime(dt_string, '%d-%m-%Y %H:%M:%S')
print(dt_value)
Python strptime 3

Let me use this Python strptime to return the DateTime from the given Hours, Minutes, Seconds. Please refer to the strftime function in Python.

from datetime import datetime
 
dt_string = '14:22:33'
time_value = datetime.strptime(dt_string, '%H:%M:%S')
print(time_value)
 
dt_str = '10-45-59'
t_value = datetime.strptime(dt_str, '%H-%M-%S')
print(t_value)
Python strptime 4

Python strptime time library

You can also utilize the time library to use this strptime. In this time instance, we use the strptime function inside the time library.

import time
 
datetime_value = time.strptime('31/12/18', '%d/%m/%y')
print(datetime_value)
Python strptime 5

strptime example 3

If you specify the abbreviates in the wrong order or use the wrong format, then the strptime function throws an error.

# Python strptime Example
 
from datetime import datetime
 
dt_string = '31/12/18'
datetime_value = datetime.strptime(dt_string, '%d %m %y')
 
print(datetime_value)
Python strptime 6

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy