How can I convert a string into a date?

Johnn Kaita

I have a date that is generated from the user side. However this date is in string format Here is an example below:

period = '12-4-2020'

how can I convert it to datetime format?

pciunkiewicz

You can use the datetime.datetime.strptime method:

period = '12-4-2020'
datetime.datetime.strptime(period, '%m-%d-%Y')

or (the order of day month year is ambiguous based on that date string)

datetime.datetime.strptime(period, '%d-%m-%Y')

The first argument is your date string, and the second argument is the string format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java?

How can I convert a date into an integer?

How can I convert a String formatted in this way into a Date using Java?

How can I convert String[] to ArrayList<String>

How can i convert String to Date when it has "TRT" in it

How can I convert string date to NSDate?

Android, How can I Convert String to Date?

In python pandas, how can I convert this formatted date string to datetime

How do I convert a string with no delimiters into a date?

VBA - How do I convert Date into String

How can I convert a custom `dayHour` string variable to date format?

In Jenkins pipeline, how can I convert a String to Date?

How can I convert "year-month-day" string of date, to day as a string in user local language

How can I convert date string to data in javascript in different timezone?

How can I convert this string to a javascript Date object?

How can I convert timestamps in a column to a date?

How can I convert this to an actual date?

How can I convert sperated date string to consecutive date string

How can I convert string date to NSDate?

How do i convert this date string to datetime?

How I can convert publishedAt to date and time

How can I convert Date JsonObject to String?

How can I convert date to string in python?

How can I convert a date format in SQL?

How can I convert an empty string value to a nullable date value?

How can I convert date to dttm in R?

How can I convert a specific string date to date or datetime in Spark?

How can I convert a specific string format to date or datetime in Spark?

how I can convert an specific character string to date time in r?