How to convert a string into datetime format - python

I'm trying to convert strings in a list to datetime format on Python. I am unable to use pd.DateTime at the moment. The imported datetime package doesn't seem to work. I'm new to this.
Please help.
Cheers.
Code Image

You should consider using official datetime formats
Example:
from datetime import datetime
#datetime(year, month, day)
date = datetime(2018, 11, 28)
# datetime(year, month, day, hour, minute, second, microsecond)
date = datetime(2017, 11, 28, 23, 55, 59, 342380)

you can use strptime
from datetime import datetime, strptime
my_datetime_list = [strptime(string_date, '%y-%m-%d') for string_date in list_of_string_dates]

Related

Convert date/time without year to this year

I'm taking input that looks like "8-15 14:45" and trying to turn it into a datetime object (and then turn it back into a string). My issue is that it sets the year to 1900, but I need the year set to this year. How do I accomplish this?
So far I have datetime.datetime.strptime('8-15 14:45', '%m-%d %H:%M') which gives me datetime.datetime(1900, 8, 15, 14, 45) but I need datetime.datetime(2021, 8, 15, 14, 45). I'd like to not hardcode 2021 if I can help it.
Apologies if this is answered somewhere, my google-fu has failed me.
Use datetime.replace with datetime.today().year:
this_year = dt.datetime.today().year
dt.datetime.strptime('8-15 14:45', '%m-%d %H:%M').replace(year=this_year)
You can use the method now() of the datetime class and only ask for the property year of the returned datetime object:
from datetime import datetime
t = datetime.now().year
print(t)
>>> 2021
You can do this by replacing the year on the parsed date like this:
from datetime import datetime as dt
date = dt.strptime('8-15 14:45', '%m-%d %H:%M')
date = date.replace(year=dt.now().year)

How to get month and year from date

I have a date in this format - 2020-01-31T00:00:00.000Z, this is coming from an API. How do I get the month (e.g. April) and year (e.g. 2020) from this date using the datetime module?
I would suggest you to use the dateutil package to parse ISO 8601 dates.
>>> from dateutil import parser
>>> parser.isoparse('2020-01-31T00:00:00.000Z')
datetime.datetime(2020, 1, 31, 0, 0, tzinfo=tzutc())
Then you can retrieve the month and year from the datetime object it returns.
If you want to do using datetime module you can do this as
from datetime import datetime
input_dt = '2020-01-31T00:00:00.000Z'
dt_object = datetime.strptime(input_dt, "%Y-%m-%dT%H:%M:%S.%fz")
Now you can do
dt_object.year
dt_object.day
dt_object.month
The strptime() method creates a datetime object from the given string.

convert str date to datetime for timezone

I have an API that return this date string
strdate = '2019-10-07T06:09:28.984Z'
How do I convert it to a datetime object?
dt = datetime.datetime.strptime(strdate, '%Y-%m-%d')
If I use strptime like above i get "ValueError: unconverted data remains: T06:09:54.346Z"
What do I do if there is "T" and "Z" included in the string date? I want the data in local time. But is the string really timezone aware so I can convert it properly? In this case I know the timezone, but what if I did not know?
The error is because you're not including the time part in the format string. Do that:
datetime.strptime('2019-10-07T06:09:28.984Z', '%Y-%m-%dT%H:%M:%S.%f%z')
This results in:
datetime.datetime(2019, 10, 7, 6, 9, 28, 984000, tzinfo=datetime.timezone.utc)
If you want to convert this to a local timezone, do that:
from pytz import timezone
dt = datetime.strptime(strdate, '%Y-%m-%dT%H:%M:%S.%f%z')
local_dt = dt.astimezone(timezone('Asia/Tokyo'))
Demo: https://repl.it/repls/RotatingSqueakyCertifications
import datetime
strdate = '2019-10-07T06:09:28.984Z'
dt=datetime.datetime.strptime(strdate, "%Y-%m-%dT%H:%M:%S.%fZ")
print(dt)
# output - 2019-10-07 06:09:28.984000

Convert python timezone aware datetime string to utc time

I have a Python datetime string that is timezone aware and need to convert it to UTC timestamp.
'2016-07-15T10:00:00-06:00'
Most of the SO links talks about getting the current datetime in UTC but not on converting the given datetime to UTC.
Hi this was a bit tricky, but here is my, probably far from perfect, answer:
[IN]
import datetime
import pytz
date_str = '2016-07-15T10:00:00-06:00'
# Have to get rid of that bothersome final colon for %z to work
datetime_object = datetime.datetime.strptime(date_str[:-3] + date_str[-2:],
'%Y-%m-%dT%H:%M:%S%z')
datetime_object.astimezone(pytz.utc)
[OUT]
datetime.datetime(2016, 7, 15, 16, 0, tzinfo=<UTC>)

how to get today's start time?

I want to get today's start time, I do this by way as follow:
from datetime import datetime
datetime.strptime(datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')
do I have better way?
You can use datetime combine
from datetime import datetime, date, time
dt = datetime.combine(date.today(), time.min)
You can just call date() on a datetime object to get just the date:
In [122]:
import datetime as dt
dt.datetime.now().date()
Out[122]:
datetime.date(2015, 9, 25)
EDIT
looking at this questions years later you can just use today from datetime.date:
In[5]:
import datetime
datetime.date.today()
Out[5]: datetime.date(2018, 11, 22)

Categories