Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a datetime string back to datetime object?

I am storing a datetime string in a database. Now I face a problem. When I fetch the string from the database, I need to convert it back to a datetime object...

Any easy way to do that?

The string of datetime looks like:

2010-11-13 10:33:54.227806
like image 856
Bin Chen Avatar asked Nov 13 '10 02:11

Bin Chen


People also ask

How do I convert a date to a datetime object?

Using the datetime() Pass the year, month and day values of the desired date object (as my_date. year, my_date. month, my_date. day) to this constructor to convert a date object to datetime object.

How do I convert a string to a datetime in Salesforce?

Example taken from the documentation: parse('10/14/2011 11:46 AM'); String myDtString = dt. format(); system. assertEquals(myDtString, '10/14/2011 11:46 AM'); This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale.

How do I change a string to a datetime in darts?

print(new DateFormat('yyyy/MM/dd'). parse(null)); That's how to parse a String to a DateTime in Dart.


1 Answers

You want datetime.strptime(date_string, format).

from datetime import datetime
datetime.strptime("2010-11-13 10:33:54.227806", "%Y-%m-%d %H:%M:%S.%f")

For details on the format string, see http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior

like image 66
Nicholas Knight Avatar answered Oct 18 '22 15:10

Nicholas Knight