This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a string '201502190759'. This represents 2015/02/19 at 7:59.
Is there a method within the python library that can convert '201502190759' into a datetime object or timestamp?
Yep, datetime.datetime.strptime will do it.
>>> import datetime
>>> print(datetime.datetime.strptime('201502190759', '%Y%m%d%H%M'))
2015-02-19 07:59:00
The docs on the format modifiers are here.
Related
This question already has answers here:
Convert string into datetime.time object
(4 answers)
Closed 1 year ago.
I have a string in the str format
PATTERN_OUT = "%H:%M"
date_time = (datetime.strftime(enddateandtime, PATTERN_OUT))
I need to convert it to datetime.time. How can this be done?
You can utilize the fact that time string is \d\d:\d\d.
Look at the following snippet.
from datetime import time
time_str = "10:01"
time(*map(int, time_str.split(':')))
Add exception handler if required.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I need to convert string to date type.What should I do?
My string is "04 Oct 2019". I need to convert to date type "04/10/2019".
Note:You can't ignore 0
import calendar
def convert_str_to_date(s):
l = s.split()
l[1] = "{0:02d}".format(list(calendar.month_abbr).index(l[1]))
return "/".join(l)
print(convert_str_to_date("04 Oct 2019"))
It won't ignore 0 as "{0:02d}".format
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have a variable '2019-05-30 21:01:09' that needs to be converted into 2019-05-30 21:01:09. What is the best way to go about this.
from datetime import datetime
strToDate = datetime.strptime('2019-05-30 21:01:09','%Y-%m-%d %H:%M:%S')
strptime() allows for the conversion of string to date time object provided that you give the format the function should expect as the second argument
You can using datetime library
from datetime import datetime
datetime.strptime('2019-05-30 21:01:09', '%Y-%m-%d %H:%M:%S')
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
This question already has answers here:
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 7 years ago.
How to convert "2013-10-21 12:00:00.004" to datetime object in Python?
The problem is there is decimal number in seconds.
here is a working example
import datetime
dt = datetime.datetime.strptime("2013-10-21 12:00:00.004", "%Y-%m-%d %H:%M:%S.%f")
print (dt.microsecond)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert Date String to DateTime Object in Python
I have a date string:
Mon Oct 15 15:05:00 UTC 2012
How to converte this string to timestamp ?
This should help:
http://docs.python.org/library/time.html#time.strptime