date from string to datetime.date in python [duplicate] - python

This question already has answers here:
Python date string to date object
(9 answers)
Closed 9 years ago.
Can this be done any way more pythonic?
>>> import datetime
>>> date = '20131018'
>>> date
'20131018'
>>> year, month, day = date[0:4], date[4:6], date[6:]
>>> datetime.date(int(year), int(month), int(day))
datetime.date(2013, 10, 18)
Thanks

Python already has a built-in way of parsing dates from strings in the datetime package, namely datetime.datetime.strptime:
>>> from datetime import datetime as dt
>>> date = '20131018'
>>> dt.strptime(date, "%Y%m%d").date()
datetime.date(2013, 10, 18)
See the docs for all of the available format / parsing options.

Related

How can I get the difference in days between today and a previous date? [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 2 years ago.
I'm trying to get the difference (in days) between today and a previous date here:
from datetime import date
today = date.today() # get today's date
print("Today's date:", today)
new_today = today.strftime("%Y, %#m, %Y") # convert it so that delta.days understands
print(new_today)
f_date = date(new_today)
l_date = date(2014, 7, 11)
delta = f_date - l_date
print(delta.days)
But I get an error:
TypeError: an integer is required (got type str)
I've read multiple threads on this, but they don't address using today's date in the calculation.
What's the best way to perform this calculation?
using datetime
import datetime
x = datetime.date.today()
y = datetime.date(2014, 7, 11)
diff = x-y
print(diff.days)
output
2392

Python: Add one day to custom date? [duplicate]

This question already has answers here:
Add 1 day to my date in Python [duplicate]
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I'm trying to add one day to custom date(in string). Time format is dd/mm/yyyy
Sample input:
'02/11/2020'
Output:
'03/11/2020'
from datetime import datetime, timedelta
date = '02/11/2020'
old = datetime.strptime(date, "%d/%m/%Y")
new = old + timedelta(days=1)
new_date = new.strftime("%d/%m/%Y")
print(new_date) # '03/11/2020'
You can use the datetime module
from datetime import datetime, timedelta
start = datetime.strptime("%d/%m/%Y", "02/11/2020")
end = start + datetime.timedelta(days = 1)

Python Convert date to datetime directly [duplicate]

This question already has answers here:
Convert date to datetime in Python
(14 answers)
Closed 2 years ago.
In Python, I convert a date to datetime by:
converting from date to string
converting from string to datetime
Code:
import datetime
dt_format="%d%m%Y"
my_date = datetime.date.today()
datetime.datetime.strptime(my_date.strftime(dt_format), dt_format)
I suspect this is far from the most efficient way to do this. What is the most efficient way to convert a date to datetime in Python?
Use datetime.datetime.combine() with a time object, datetime.time.min represents 00:00 and would match the output of your date-string-datetime path:
datetime.datetime.combine(my_date, datetime.time.min)
Demo:
>>> import datetime
>>> my_date = datetime.date.today()
>>> datetime.datetime.combine(my_date, datetime.time.min)
datetime.datetime(2013, 3, 27, 0, 0)
Alternatively, as suggested here, this might be more readable:
datetime(date.year, date.month, date.day)

How to convert API timestamp into python datetime object [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 8 years ago.
I am getting the following string from an API call:
s = '2014-12-11T20:46:12Z'
How would I then convert this into a python object? Is there an easy way, or should I be splitting up the string, for example:
year = s.split('-')[0]
month = s.split('-')[1]
day = s.split('-')[2]
time = s.split('T')[1]
...etc...
You can use the datetime.datetime.strptime function:
>>> from datetime import datetime
>>> s = '2014-12-11T20:46:12Z'
>>> datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2014, 12, 11, 20, 46, 12)
>>>
For a complete list of the available format codes, see strftime() and strptime() Behavior.
Using datetime should do it, recently I found arrow is also a good library to deal with dates.
import arrow
s = '2014-12-11T20:46:12Z'
your_date = arrow.get(s)
print(t.year) # 2014
print(t.hour) # 20

convert string to date type python [duplicate]

This question already has answers here:
Python date string to date object
(9 answers)
Closed 9 years ago.
How do I convert a string to a date object in python?
The string would be: "30-01-12" (corresponding to the format: "%d-%m-%y")
I don't want a datetime.datetime object, but rather a datetime.date
You still use datetime.datetime but then request just the .date() portion:
datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()
Demonstration:
>>> import datetime
>>> datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()
datetime.date(2012, 1, 30)
This should work:
import datetime
s = "30-01-12"
slist = s.split("-")
sdate = datetime.date(int(slist[2]),int(slist[0]),int(slist[1]))
from datetime import datetime,date
date_str = '30-01-12'
formatter_string = "%d-%m-%y"
datetime_object = datetime.strptime(date_str, formatter_string)
date_object = datetime_object.date()

Categories