I want to convert
2010-03-01 to 733832
I just found this toordinal code
d=datetime.date(year=2010, month=3, day=1)
d.toordinal()
from this
But i want something more like
d=datetime.date('2010-03-01')
d.toordinal()
Thanks in advance
You'll need to use strptime on the date string, specifying the format, then you can call the toordinal method of the date object:
>>> from datetime import datetime as dt
>>> d = dt.strptime('2010-03-01', '%Y-%m-%d').date()
>>> d
datetime.date(2010, 3, 1)
>>> d.toordinal()
733832
The call to the date method in this case is redundant, and is only kept for making the object consistent as a date object instead of a datetime object.
If you're looking to handle more date string formats, Python's strftime directives is one good reference you want to check out.
like this:
datetime.strptime("2016-01-01", "%Y-%m-%d").toordinal()
You need to firstly convert the time string to datetime object using strptime(). Then call .toordinal() on the datetime object
>>> from datetime import datetime
>>> date = datetime.strptime('2010-03-01', '%Y-%M-%d')
>>> date.toordinal()
733773
It is even better to create a function to achieve this as:
def convert_date_to_ordinal(date):
return datetime.strptime(date, '%Y-%M-%d').toordinal()
convert_date_to_ordinal('2010-03-01')
#returns: 733773
Related
This is my code - I want to convert a string into a time in Python - it sort of works:
import datetime
firstTime = ("18:08:14")
firstTime = datetime.datetime.strptime(firstTime, "%H:%M:%S")
print (firstTime)
The problem is, I get '1900-01-01 18:08:14' instead of just '18:08:14'. I know this is a fairly basic thing, but I'm new to Python and any help would be appreciated.
As my comment suggests, use a time class rather than datetime since you don't need the date part:
>>> from datetime import datetime
>>> firsttime = datetime.strptime('18:08:14','%H:%M:%S')
>>> print(firsttime)
1900-01-01 18:08:14
>>> print(firsttime.time())
18:08:14
or simply:
>>> firsttime = datetime.strptime('18:08:14','%H:%M:%S').time()
>>> print(firsttime)
18:08:14
try this..
>>> from datetime import datetime
>>> date=datetime.now()
>>> date.strftime('%H:%M:%S')
'23:55:17'
>>>
Your code seems fine. Just change strptime to strftime.
import datetime
firstTime = ("18:08:14")
firstTime = datetime.datetime.strftime(firstTime, "%H:%M:%S")
print (firstTime)
strptime takes a string and convert to datetime object.
strftime creates formatted string from given date,time,datetime object according to a specific format
How can I retrieve a date object (NOT datetime object) from the datetime class in %m/%d/%y format?
You'd just format the datetime with that format, using the datetime.datetime.strftime() method:
date_string = dt.strftime('%m/%d/%y')
You don't have to use the time components at all when formatting a string.
If you wanted a date object (a datetime.date() instance), call the datetime.datetime.date() method:
date_object = dt.date()
Note that datetime.date() instances support string formatting too.
Demo:
>>> from datetime import datetime
>>> dt = datetime.now()
>>> dt.strftime('%m/%d/%y')
'07/09/15'
>>> dt.date()
datetime.date(2015, 7, 9)
>>> dt.date().strftime('%m/%d/%y')
'07/09/15'
I have a string that contains the date in this format:
full_date = "May.02.1982"
I want to use datetime.strptime() to display the date in all digits like: "1982-05-02"
Here's what I tried:
full_date1 = datetime.strptime(full_date, "%Y-%m-%d")
When I try to print this, I get garbage values like built-in-67732
Where am I going wrong? Does the strptime() method not accept string values?
Your format string is wrong, it should be this:
In [65]:
full_date = "May.02.1982"
import datetime as dt
dt.datetime.strptime(full_date, '%b.%d.%Y')
Out[65]:
datetime.datetime(1982, 5, 2, 0, 0)
You then need to call strftime on a datetime object to get the string format you desire:
In [67]:
dt.datetime.strptime(full_date, '%b.%d.%Y').strftime('%Y-%m-%d')
Out[67]:
'1982-05-02'
strptime is for creating a datetime format from a string, not to reformat a string to another datetime string.
So you need to create a datetime object using strptime, then call strftime to create a string from the datetime object.
The datetime format strings can be found in the docs as well as an explanation of strptime and strftime
I'm working on an image upload utility, and part of the functionality is to parse the IPTC and EXIF data of the images.
IPTCInfo gets the information I need, but the date fields are in the format 20130925.
Now, I can break that integer up into 2013 09 25 and create a date object. Before I do so, is there already existing functionality to solve this issue?
The date class doesn't have a string-parsing function, but the datetime class does, strptime.
So, first make a datetime, then extract the date part of it:
>>> s = '20130925'
>>> dt = datetime.datetime.strptime(s, '%Y%m%d')
>>> d = dt.date()
>>> d
datetime.date(2013, 9, 25)
If you don't understand where the '%Y%m%d' comes from, see strftime() and strptime() Behavior.
You can use datetime.strptime:
>>> import datetime
>>> datetime.datetime.strptime("20130925","%Y%m%d").date()
datetime.date(2013, 9, 25)
What are these date-time formats? I need to convert them to the same format, to check if they are the same. These are just two coming from a separate data source, so I need to find a way to make them the same format. Any ideas?
2013-07-12T07:00:00Z
2013-07-10T11:00:00.000Z
Thanks in advance
That extra .000 is micro seconds.
This will convert a date string of a format to datetime object.
import datetime
d1 = datetime.datetime.strptime("2013-07-12T07:00:00Z","%Y-%m-%dT%H:%M:%SZ")
d2 = datetime.datetime.strptime("2013-07-10T11:00:00.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
Then convert them into any format depending on your requirement, by using:
new_format = "%Y-%m-%d"
d1.strftime(new_format)
perhaps use .isoformat()
string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM]
>>> import datetime
>>> datetime.datetime.utcnow().isoformat() + "Z"
'2013-07-11T22:26:51.564000Z'
>>>
Z specifies "zulu" time or UTC.
You can also add the timezone component by making your datetime object timezone aware by applying the appropriate tzinfo object. With the tzinfo applied the .isoformat() method will include the appropriate utc offset in the output:
>>> d = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
>>> d.isoformat()
'2019-11-11T00:52:43.349356+00:00'
You can remove the microseconds by change the microseconds value to 0:
>>> no_ms = d.replace(microsecond=0)
>>> no_ms.isoformat()
'2019-11-11T00:52:43+00:00'
Also, as of python 3.7 the .fromisoformat() method is available to load an iso formatted datetime string into a python datetime object:
>>> datetime.datetime.fromisoformat('2019-11-11T00:52:43+00:00')
datetime.datetime(2019, 11, 11, 0, 52, 43, tzinfo=datetime.timezone.utc)
http://www.ietf.org/rfc/rfc3339.txt
you can try to trim the string
data = "2019-10-22T00:00:00.000-05:00"
result1 = datetime.datetime.strptime(data[0:19],"%Y-%m-%dT%H:%M:%S")
result2 = datetime.datetime.strptime(data[0:23],"%Y-%m-%dT%H:%M:%S.%f")
result3 = datetime.datetime.strptime(data[0:9], "%Y-%m-%d")
use datetime module.
For a variable
import datetime
def convertDate(d):
new_date = datetime.datetime.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
return new_date.date()
convertDate("2019-12-23T00:00:00.000Z")
you can change the ".date()" to ".year", ".month", ".day" etc...
Output: # is now a datetime object
datetime.date(2019, 12, 23)
For a DataFrame column, use apply()
df['new_column'] = df['date_column'].apply(convertDate)
* Short and best way:
str(datetime.datetime.now()).replace(' ','T')
or
str(datetime.datetime.now()).replace(' ','T') + "Z"