I am using python3 and I have a time data format like '01-FEB-17' and I need to parse it with datetime.strptime. Does anyone know which format is the correct for this? I tried '%d-%b-%Y', '%d-%m-%Y',... and none of them worked.
Thanks
You can use this
from datetime import datetime
t = datetime.strptime('01-FEB-17', '%d-%b-%y')
NOTE: You can head here for a full list of format specifiers to use
Related
I have the follow code, but having trouble removing the Z at the end. The time format looks like this 2020-07-28T15:47:36.165310258Z
from datetime import datetime
datetime.strptime(f[a]['Created On'], "%Y-%m-%dT%H:%M:%S.%f")
When using the %f, I noticed that it is only able to the next 6 digit. Is there any way to make it take more? This is the error that I am getting (MISSING) ValueError: unconverted data remains: 258Z
What is the best way to remove the Z and have it just return the date?
Was able to get around the issue by just moving the last 4 characters from the string and then using datetime.strptime on it.
datetime.strptime(f[a]['Created On'][:-4], "%Y-%m-%dT%H:%M:%S.%f")
I have the following code which gives the following output:
print(df1['Diff'].mean())
outputs:
10 days 16:13:29.467455
But since i just want the days value and not the time, i have done this:
print(datetime.strptime(df1['Diff'].mean(), format ='%d')
but i am getting the following error:
^
SyntaxError: unexpected EOF while parsing
Why am i getting this error?
For date, time, and datetime objects
You should be using strftime to format the time, not to parse the time (as in strptime).
print(obj.strftime('%d'))
strptime expects a string to be passed in (and you were passing in a datetime object), whereas strftime formats an existing datetime object.
For timedelta objects
print(obj.days)
This gets the days counterpart you're looking for.
I think the instance of df1['Diff'].mean() is str and datetime.strptime() can be use only in datetime methods. So to only get date you have to take slice of df1['Diff'].mean() like df1['Diff'].mean()[:-14]
Which is in your case.
I am given the following object:
datetime.time(18, 50)
I want to convert to a string:
"18:50:00"
Please advise how can I do this?
Tried strftime but getting errors.
datetime.time(18, 50).strftime("%H:%M:%S")
You need to do:
from datetime import time
print(time(18, 50).strftime("%H:%M:%S"))
Importing the time function from datetime module seems to be different from datetime instance's time method.
I need your help.
I have parsed a web site, and I have harvested this:
2018-08-18T23:31:00
I searched, but couldn't find how to change datetime to timestamp
Example of my desired result :
1535414693077
The previous answer is incorrect as there is no '%s' formatter in datetime, even though it works, sometimes. See this answer for how wrong it is to use '%s'.
from datetime import datetime
import pytz
pytz.utc.localize(datetime.strptime('2018-08-18T23:31:00', '%Y-%m-%dT%H:%M:%S')).timestamp()
Output (float): 1534635060.0
Excuse my lack of knowledge playing with time in python3 but I am stuck for 30 minutes already in this problem so any help will be highly appreciated.
I have salesforce time coming in my script as 2016-11-15T23:49:48.000Z. How can I convert it into mysql datetime format without storing it in a variable.
Desired output format: 2016-11-16 12:11:21.525885
You can use the time module's strptime referenced here.
I'm making some assumptions about the input data, but here's a format that will probably work:
import time
import datetime
def translate_timestamp(timestamp):
t = time.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
s = datetime.datetime.fromtimestamp(time.mktime(t))
return s.strftime("%Y-%m-%d %H:%M:%S.%f")
An example might be:
>>> translate_timestamp("2016-11-15T23:49:48.000Z")
'2016-11-15 23:49:48.000000'