I get from third-party service string with time. It looks like PT8H30M. What is the time format and how it convert into datetime object in Python 2.7
If we assume that the format gives only hours and minutes, you can use datetime.datetime.strptime() to parse these out, and return a datetime.time object from the result:
>>> from datetime import datetime
>>> datetime.strptime('PT8H30M', 'PT%HH%MM').time()
datetime.time(8, 30)
Related
I have a string "15:15:00"
I need to convert it to timestamp like 1410748201
Python
A UNIX timestamp always needs a relation between a date and a time to actually form it, since it is the number of seconds that are passed since 1970-01-01 00:00. Therefore I would recommend you to use the datetime module.
Let's assume you have given the following format: "2022-10-31 15:15:00".
With datetime you can convert the string into a datetime object by using the strptime() function.
Afterwards, a datetime object gives you the ability to convert your datetime into a UNIX timestamp with the timestamp() method of your datetime object.
from datetime import datetime
datetime_str = "2022-10-31 15:15:00"
datetime_obj = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
print(datetime_obj.timestamp())
I have the following dates:
4/29/2020
5/7/2020
9/10/2020
10/5/2020
11/20/2020
The dates extracted from Oracle are correctly read as datetime objects in Python. However, when I manually add dates to the list in Excel, the program sees the date as string and crashes.
invalid input for query argument $1: '9/10/2020' (expected a datetime.date or datetime.datetime instance, got 'str')
This is what I am doing:
if isinstance(my_date,str):
my_date = date.fromisoformat(my_date)
It's not working. Is there a way to automatically convert a date in any format to datetime object? Thanks!
You can convert your code to something like this:
from datetime import datetime
if isinstance(my_date,str):
my_date = datetime.strptime(my_date, '%m/%d/%Y')
Yes there is : datetime.strptime
You can find documentation on how to use it here : https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
I'm using an API that sends a date in a wierd format "YYYY-MM-DDTHH:MM:SSZ".
So the date comes out as 2018-04-27T23:59:18Z, I have never seen a date and time formatted like this. Its a string and I would like to format it as MM-DD-YYYY HH:MM:SS. I can't even wrap my head around removing the T and Z. Any help would be appreciated! Thanks in advance!
Create datetime.datetime object from your string via datetime.strptime, then turn it back into a string with its strftime method.
>>> from datetime import datetime
>>> s = "2018-04-27T23:59:18Z"
>>> datetime.strptime(s, '%Y-%m-%dT%XZ').strftime('%m-%d-%Y %X')
>>> '04-27-2018 23:59:18'
strptime and strftime behavior
(Depending on you locale you might have to use %H, %M and %S instead of %X.)
That looks like the ISO 8601 time format.
For reasons that I really don't understand, there's no standard library function that parses ISO 8601 correctly. The dateutil package that you can find on PyPI will parse it for you though.
Use this pattern:
import datetime
d = '2018-04-27T23:59:18Z'
myDate = datetime.datetime.strptime(d, '%Y-%m-%dT%H:%M:%SZ')
# datetime.datetime(2018, 4, 27, 23, 59, 18)
Then to get a datetime string use strftime:
myDate_str = myDate.strftime('%Y-%m-%d %H:%M:%S')
# '2018-04-27 23:59:18'
I am having problem getting some datetime conversions. I am actually using django and in one of model field I used date field not datetime field. Now I need to show time difference and I got the snippet for that from djangosnippets. But that accepts timestamp.
So I am confuse that what actually is python timestamp and how can it be made. I tried different things as in below lines:
publish_date.timetuple()
but this says, that 'datetime.date' object has no attribute 'mktime'.I guess datetime.datetime has this mktime but not datetime.date so I can't use that.
int(parse(publish_date).strftime('%s'))
Here parse is from a third party module named dateutil but this time it gives following error.
datetime.date' object has no attribute 'read'
And it gives same error for following code:
humanizeTimeDiff(publish_date)
So any better idea or approach that I should try to convert datetime.date object to timestamp. It seems like converting datetime.datetime would be also helpful. But how ?
Since you don't have any time information in your database (only date), you can only calculate the difference in number of days. To do that, simply subtract the date objects. This will yield a timedelta object which represents the difference between two date (or datetime) objects.
>>> d1
datetime.date(2012, 9, 16)
>>> d2
datetime.date(2012, 9, 17)
>>> d2-d1
datetime.timedelta(1)
>>> print d2-d1
1 day, 0:00:00
>>> print (d2-d1).days
1
I need to convert a date from a string (entered into a url) in the form of 12/09/2008-12:40:49. Obviously, I'll need a UNIX Timestamp at the end of it, but before I get that I need the Date object first.
How do I do this? I can't find any resources that show the date in that format? Thank you.
You need the strptime method. If you're on Python 2.5 or higher, this is a method on datetime, otherwise you have to use a combination of the time and datetime modules to achieve this.
Python 2.5 up:
from datetime import datetime
dt = datetime.strptime(s, "%d/%m/%Y-%H:%M:%S")
below 2.5:
from datetime import datetime
from time import strptime
dt = datetime(*strptime(s, "%d/%m/%Y-%H:%M:%S")[0:6])
You can use the time.strptime() method to parse a date string. This will return a time_struct that you can pass to time.mktime() (when the string represents a local time) or calendar.timegm() (when the string is a UTC time) to get the number of seconds since the epoch.