
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
More Posts related to Python,
- How to add two float numbers in Python
- tkinter - Hello World! Program
- Convert Float to String in Python
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- How to List all Packages installed using pip [Python]
- Check if String Contains a Substring - Python
- Change label (text) color in tkinter
- Where does brew install python in macOS
- Validate email address in Python using regular expression (regex)
- How to uninstall pip Python packages
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Fix command not found pip or pip3 on zsh shell
- Check version of pip package installer for Python
- 3 Python program to add two numbers
- Calculate discount amount python code
- How to add borders to tkinter label text
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Comments in Python Programming
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to take user input from the console in a Python program
- Install and Run Jupyter Notebook on Mac (macOS)
- Tkinter - add x and y padding to label text
- 7 Python Arithmetic Operators with Examples [Tutorial]
- Python Sleep Function/Method Code Example
More Posts:
- vi undo redo command [Examples] - Mac-OS-X
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated - Java
- Eclipse Error The JVM Shared Library JavaVirtualMachines does not contain the JNI_CreateJavaVM symbol - Eclipse
- Python Hello World! Program with code example (snippet) - Python
- How to remove blank lines from a file using Notepad++ - NotepadPlusPlus
- Convert Float to String in Python - Python
- JSON Syntax : JSON Tutorial - Json-Tutorial
- Change color of macOS terminal prompt - MacOS