Answers for "typecast string to datetime"

46

string to datetime convert

import datetime

date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')

print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
Posted by: Guest on June-01-2020
0

Convert String to DateTime

import pandas as pd

# Define string
date = '04/03/2021 11:23'

# Convert string to datetime format
date1 = pd.to_datetime(date)

# print to_datetime output
print(date1)

# print day, month and year separately from the to_datetime output
print("Day: ", date1.day)
print("Month", date1.month)
print("Year", date1.year)
Posted by: Guest on February-22-2022

Python Answers by Framework

Browse Popular Code Answers by Language