
Python String to DateTime Example
In order to convert a String to a DateTime object in Python we need to make use of the datetime module,
Example:import datetime
string_date1 = '2022-03-22'
print(string_date1)
datetime_object1 = datetime.datetime.strptime(string_date1, '%Y-%m-%d')
print(datetime_object1)
string_date2 = '2022-03-22 10:10:12.345678'
print("String: " +string_date2)
datetime_object2 = datetime.datetime.strptime(string_date2, '%Y-%m-%d %H:%M:%S.%f')
print(datetime_object2)
Output:
2022-03-22
2022-03-22 00:00:00
String: 2022-03-22 10:10:12.345678
2022-03-22 10:10:12.345678
-
Have Questions? Post them here!
More Posts related to Python,
- Check version of pip package installer for Python
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Indent Python code in Notepad++
- Change label (text) color in tkinter
- Float built-in function in Python
- [Fix] ValueError: substring not found in Python
- pip get list of all outdated Python packages
- Calculate discount amount python code
- 3 Python program to add two numbers
- How to List all Packages installed using pip [Python]
- Python range() function examples
- How to resolve Failed to create interpreter PyCharm Error
- Format Python Code in Visual Studio Code (VS Code)
- Validate email address in Python using regular expression (regex)
- Install and Run Jupyter Notebook on Mac (macOS)
- How to delete a file using Python code example
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- Change the background of Tkinter label or text
- Comments in Python Programming
- Take input argument from command line in Python Programming
- How to Convert Python String to DateTime Object
- Tkinter - add x and y padding to label text
- [Tutorial] Install Python on Visual Studio Code (VS Code)
More Posts:
- Microsoft Teams - Where would you like to start - Business or Personal - Teams
- Steps to Integrate Latest Facebook SDK with your Android Application - Facebook
- Limit scrollback rows in macOS Terminal - MacOS
- Docker - Error response from daemon: You cannot remove a running container - Docker
- macOS Big Sur java.lang.UnsatisfiedLinkError CoreFoundation - Android Studio - Android-Studio
- Sort a List using Java 8 Stream Examples - Java
- Create a Gradle Java Project in VS Code - Gradle
- YAML Parser using Java Jackson Library Example - Java