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.
Related
This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 3 years ago.
I have a datetime object
v = 21.01.2019 14:25:37
I want to convert above datetime object to date as this
a = convert(v)
a = 21.01.2019
how i can do it
If you want today's date.try this :
datetime.datetime.now().date()
If you want your datetime object date :
v.date()
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:
Parsing datetime strings containing nanoseconds
(5 answers)
Closed 4 years ago.
I have a date formated like this: "2018-06-12T13:58:36.663550655Z"
I want be convert this string into a Unix time stamp.
This is my code:
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
time = datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
unixTime = datetime.utcfromtimestamp(time)
When i run it, there's a error:
ValueError: time data '2018-06-12T14:03:35.306662173Z' does not match
format '%Y-%m-%dT%H:%M:%S.%fZ'
Would be nice if someone could help me !
Thanks!
Try stripping the extra info.
Ex:
import time
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
d = datetime.strptime(date[:26],"%Y-%m-%dT%H:%M:%S.%f")
unixTime = time.mktime(d.timetuple())
print(unixTime)
Output:
1528792415.0
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 an answer here:
Python converting datetime to be used in os.utime
(1 answer)
Closed 6 years ago.
I'm trying to convert an ordinal dateTime value into a value that can be accepted by os.utime()
My date has the format: 201642322295
(which is: year=2016, month=4, day=23, hour=22, minute=29, second=5)
However, the method won't accept this and I'm not sure how to/ what to convert it into. I've tried converting it into a datetime also but this does not work.
The code segment:
s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
os.utime(self.fileName, (date,date))
(I had tried using just the ordinal format, which modifies the datetime of the file but is not at all correct)
edit: This is not the same as 'python converting datetime to be used in os.utime' because it's using a completely different format
os.utime expects values in the form of integers representing a untix timestamp.
s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
utime = time.mktime(date.timetuple())
os.utime(self.fileName, (utime, utime))