Program Python untuk Mengonversi String ke Datetime

Contoh 1: Menggunakan modul datetime

from datetime import datetime

my_date_string = "Mar 11 2011 11:31AM"

datetime_object = datetime.strptime(my_date_string, '%b %d %Y %I:%M%p')

print(type(datetime_object))
print(datetime_object)

Keluaran

<class 'datetime.datetime'>
2011-03-11 11:31:00

Jika Anda ingin mempelajari lebih lanjut tentang arahan dan strptime (), silakan buka Python strptime () – string to datetime object.


Contoh 2: Menggunakan modul dateutil

from dateutil import parser

date_time = parser.parse("Mar 11 2011 11:31AM")

print(date_time)
print(type(date_time))

Keluaran

2011-03-11 11:31:00
<class 'datetime.datetime'>

Leave a Reply

Your email address will not be published. Required fields are marked *