
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to convert a Python date string to a date object?
In this article we convert a date string to a date object. We use the strptime() method to convert a date string to a date object.
Strptime()
The strptime() method takes the date as an input and converts it into a date object. Note that you cannot convert any string into a datetime object. The string should be in a certain format.
Syntax
The syntax of the strptime() method is shown in this section.
datetime.strptime(time_data, format_data)
Where,
time_date – it is the time present in string format.
format_date – it is the data(string) present in datetime format which is converted from time_data using this function.
Few important format codes
Format Code | Meaning | Example |
---|---|---|
%a | Shortened weekday name. | Mon, Tue,…. |
%A | Complete weekday name. | Monday, Tuesday,…. |
%w | Weekday as a decimal value. | 0,1,2…. |
%d | Day of the month as a zero-padded decimal. | 01,02,…,30 |
%-d | Day of the month as a decimal number. | 1,2,…….,30 |
%b | Shortend Month name. | Jan,Feb,… |
%B | Complete Month name. | January, February,…. |
%m | Month as a zero-padded decimal number. | 01,02,03,……,12 |
%-m | Month as a decimal number. | 1,2,3,…….,12. |
%y | Year without century as a zero-padded decimal number. | 00,01,…. |
%Y | Year with century as a decimal number. | 2001,2002,…. |
%-y | Year without century as a decimal number. | 0,1,2,3,……. |
%H | Hour(24-hour clock) as a zero-padded decimal number. | 01,02,…..,24 |
%-H | Hour(24 hour clock) as a decimal number. | 1,2,….,24. |
%I | Hour(12-hour clock) as a zero-padded decimal number. | 01,02,….,12 |
%-I | Hour(12 hour clock) as a decimal number. | 1,2,…,12. |
%p | AM or PM | AM,PM. |
%M | Minute as a zero padded decimal number. | 00,01,02,….,59 |
%-M | Minute as a decimal number. | 1,2,…..,59. |
%S | Second as a zero-padded decimal number. | 01,02,….,59 |
%-S | Second as a decimal number. | 1,2,…..,59. |
%f | Microsecond as a decimal number, zero padded on the left side | 000000,000001,….999999 |
%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
Example 1
In the following example we convert a string date to a datetime object by using the strptime. And we also extract the microsecond, minutes, and seconds.
from datetime import datetime time_data = "23/01/01 04:35:7.123" format_data = "%d/%m/%y %H:%M:%S.%f" date = datetime.strptime(time_data, format_data) print("Microsecond:",date.microsecond) print("Hour:",date.hour) print("Minute:",date.minute) print("Second:",date.second) print("The date is:",date)
Output
The output received is as follows.
Microsecond: 123000 Hour: 4 Minute: 35 Second: 7 The date is: 2001-01-23 04:35:07.123000
Example 2
The module is imported in this first example, and the input DateTime string is provided. Now we use the strptime() method to get the needed format and date() to retrieve the date from DateTime.
import datetime date_str = '29122017' # The date - 29 Dec 2017 format_str = '%d%m%Y' # The format datetime_obj = datetime.datetime.strptime(date_str, format_str) print(datetime_obj.date())
Output
The output produced is shown below.
2017-12-29
- Related Articles
- How to convert a JavaScript date object to a string?
- How to convert a string in to date object in JavaScript?
- How to convert a String into a Date object using JDBC API?
- How to convert a date object to string with format hh:mm:ss in JavaScript?
- How to convert an integer into a date object in Python?
- How to convert a Date object to LocalDate object in java?
- How to convert a string to date in MySQL?
- How to create JavaScript Date object from date string?
- Convert date string to timestamp in Python
- How to convert a MySQL date to JavaScript date?
- Java Program to convert a String to Date
- How to convert a Date value to string in JDBC?
- How to convert ISO 8601 string to Date/time object in Android?
- How to convert unix timestamp string to readable date in Python?
- How to convert Python date string mm/dd/yyyy to datetime?
