Converting string with decimals into datetime [duplicate] - python

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 4 years ago.
This question is very similar to this SO, but my data is on the form:
'13-04-2018 14:06:26.866'
'13-04-2018 14:06:27.131'
'13-04-2018 14:06:27.404'
'13-04-2018 14:06:27.674'
...
i.e. the seconds are given as decimals. My reading of the datetime documentation suggests that it doesn't support this format, so I am not sure how to best proceed.

Looks like you need
.%f == Microsecond as a decimal number, zero-padded on the left.
Ex:
import datetime
s = '13-04-2018 14:06:26.866'
print(datetime.datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f"))
Output:
2018-04-13 14:06:26.866000

Related

How to change format from str to datetime in python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 1 year ago.
I have a string like this:
'2021-07-24'
I want to change this to datetime type like this:
2021-07-24T00:00:00.000+09:00
How to replace str with datetime like that format?
You can try this:
from datetime import datetime, timezone, timedelta
dt_obj = datetime.fromisoformat('2021-07-24').replace(tzinfo=timezone(timedelta(hours=9))).isoformat()
print(dt_obj)
Output:
2021-07-24T00:00:00+09:00

Numeric date changed to String [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
How to convert a date string to different format [duplicate]
(2 answers)
Closed 3 years ago.
I need help creating a function that takes a user input(Numeric Date) and turns it into a string
For Example:
input: 2018-06-20
output: June 20th 2018
any hints or code would help me out. Thank you
import datetime
d = '2018-06-20'
datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%B %d %Y')
Output:
June 20 2018
For more formatting you can look here

int to datetime in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parsing datetime in Python..?
(2 answers)
Closed 4 years ago.
I'm receiving data from the port.
This data has date and time.But not formatted.
Example:
'02012019', '090253'
I want to save it to the database and I want to convert this format.
02-01-2019 09:02:53
Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.
Anybody have a suggestion?
from datetime import datetime
date_data ='02012019 09:02:53'
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')
# output '02-01-2019 09:02:53'

Parse this string "20160713161212" to datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have datetime a string "20160713161212".
It is a datetime in string with format YYYYMMDDHHIISS
How to parse it to string with format "07/13/2016 16:12:12" in Python?
I think that's what you are looking for:
from datetime import datetime
dt = datetime.strptime('20160713161212', '%Y%m%d%H%M%S')
new_dt = dt.strftime("%Y/%m/%d %H:%M:%S")
For further info you can take a look here

String to DateTime Object in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 9 years ago.
I have date in string:
mystring = "2013-10-25 07:53:07.367857"
Can I convert this string to datetime object?
Yes, you want
from datetime import datetime
datetime.strptime(mystring, "%Y-%m-%d %H:%M:%S.%f")

Categories

Resources