I need to convert string to date in python [duplicate] - python

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

Related

How can I change date stamps in python [duplicate]

This question already has answers here:
How to Convert Datetime to String in Python?
(3 answers)
How do I turn a python datetime into a string, with readable format date?
(8 answers)
Convert datetime object to a String of date only in Python
(15 answers)
Closed 9 months ago.
how can I convert the date format from
Current format : [day] [month] [day value] [hour]:[minute]:[second] [time zone difference] [year]
to
New format : [year]-[month value]-[day value] [hour]:[minute]:[second]
For example, a current format value:
Tue Feb 04 17:04:01 +0000 2020
should be converted to:
2020-02-04 17:04:01
in python
You can use parser. Then use strftime to format the datetime object
from dateutil import parser
x = 'Tue Feb 04 17:04:01 +0000 2020 '
y = parser.parse(x).strftime('%Y-%m-%d %T')
>>> y
'2020-02-04 17:04:01'
Check out:
http://strftime.net/

How to convert string to datetime? [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
how to convert a string date into datetime format in python? [duplicate]
(2 answers)
Python: How to convert string into datetime [duplicate]
(3 answers)
Closed 5 years ago.
I have the following string:
'2017-08-15T13:34:35Z'
How to convert this string to object that I can call .isoformat()?
someobject = convert('2017-08-15T13:34:35Z')
someobject.isoformat()
How to implement convert()?
Here to parse a string to date time, then you can:
def convert(s):
return datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')
someobject = convert('2017-08-15T13:34:35Z')
print(someobject.isoformat())
You can use dateutil's parser:
>>> import dateutil.parser
>>> date = dateutil.parser.parse('2017-08-15T13:34:35Z', ignoretz=True)
>>> date
datetime.datetime(2017, 8, 15, 13, 34, 35)
>>> date.isoformat()
'2017-08-15T13:34:35'

"2013-10-21 12:00:00.004" string convert to datetime in Python [duplicate]

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)

Convert string into datetime timestamp [duplicate]

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.

How to convert string to timestamp? [duplicate]

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

Categories