How to convert string 2021-09-30_1 to datetime - python

How can I convert string 2021-09-30_1 to datetime 2021/09/30 00:00, which means that from the last string we have to substract one to get the hour.
I tried datetime.strptime(date, '%Y %d %Y %I')

datetime.strptime if to define the timestamp from a string, the format should match the provided one. datetime.strftime (note the f) is to generate a string from a datetime object.
You can use:
datetime.strptime(date, '%Y-%m-%d_%H').strftime('%Y/%m/%d %H:%M')
output: '2021/09/30 01:00'
in case the _x defines a delta:
from datetime import datetime, timedelta
d, h = date.split('_')
d = datetime.strptime(d, '%Y-%m-%d')
h = timedelta(hours=int(h))
(d-h).strftime('%Y/%m/%d %H:%M')
output: '2021/09/29 23:00'

Considering the _1 is hour and appears in al of your data (The hour part takes value between [1, 24]), your format was wrong.
For reading the date from string you'll need format it correctly:
from datetime import datetime, timedelta
date = "2021-09-30_1"
date_part, hour_part = date.split("_")
date_object = datetime.strptime(date_part, '%Y-%m-%d') + timedelta(hours=int(hour_part) - 1)
Now you have the date object. And you can display it as:
print(date_object.strftime('%Y/%m/%d %H:%M'))

from datetime import datetime
raw_date = "2021-09-30_1"
date = raw_date.split("_")[0]
parsed_date = datetime.strptime(date, '%Y-%m-%d')
formated_date = parsed_date.strftime('%Y/%m/%d %H:%M')
strptime is used for parsing string and strftime for formating.
Also for date representation you should provide format codes for hours and minutes as in:
formated_date = parsed_date.strftime('%Y/%m/%d %H:%M')

Related

Converting string to datetime [hh:mm:ss]

I have two columns Date_x and Date_y. I would like to compare them (i.e Date_x + 1 hour < Date_y)
Format of the strings looks as follows "2020-01-29 11:31:32.754292 UTC"
I have tried converting it using datetime:
from datetime import datetime as dt
df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') for x in df['Date_x']]
However, it throws an error regarding the UTC part. I tried removing it with no avail.
Last traceback:
time data '2020-01-29 18:30:28' does not match format '%Y-%m-%d %H:%M:%S.%f'
How would you go about converting the string to hh:mm:ss only?
You could use an if statement:
df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') if '.' in x else dt.strptime(x, '%Y-%m-%d %H:%M:%S') for x in df['Date_x']]
But why not just pd.to_datetime:
df["Date_x"] = pd.to_datetime(df["Date_x"], infer_datetime_format=True)

format 01-01-16 7:43 string to datetime

I have the following strings that I'd like to convert to datetime objects:
'01-01-16 7:43'
'01-01-16 3:24'
However, when I try to use strptime it always results in a does not match format error.
Pandas to_datetime function nicely handles the automatic conversion, but I'd like to solve it with the datetime library as well.
format_ = '%m-%d-%Y %H:%M'
my_date = datetime.strptime("01-01-16 4:51", format_)
ValueError: time data '01-01-16 4:51' does not match format '%m-%d-%Y %H:%M'
as i see your date time string '01-01-16 7:43'
its a 2-digit year not 4-digit year
that in order to parse through a 2-digit year, e.g. '16' rather than '2016', a %y is required instead of a %Y.
you can do that like this
from datetime import datetime
datetime_str = '01-01-16 7:43'
datetime_object = datetime.strptime(datetime_str, '%m-%d-%y %H:%M')
print(type(datetime_object))
print(datetime_object)
give you output 2016-01-01 07:43:00
First of all, if you want to match 2016 you should write %Y while for 16 you should write %y.
That means you should write:
format_ = '%m-%d-%y %H:%M'
Check this link for all format codes.

How to extract date from the string format timestamp?

I want to extract the date (eg.2018-07-16) from strings (eg. 2018-07-16 10:17:53.460035).
The strings have two formats: "2018-07-16 10:17:53.460035" and "2018-05-20 14:37:21".
When I use strptime(d, "%Y-%m-%d %H:%M:%S.%f") to convert the strings before extracting the date, it pops this error:
ValueError: time data '2018-05-20 14:37:21' does not match format
%Y-%m-%d %H:%M:%S.%f'
How can I convert both time formats to DateTime type and extract date from it?
Use to_datetime from pandas.
import pandas as pd
a = "2018-07-16 10:17:53.460035"
b = "2018-05-20 14:37:21"
print(pd.to_datetime(a).date())
print(pd.to_datetime(b).date())
You don't need the .%f at the end for the first format, that is what is causing the format error.
t = "2018-05-20 14:37:21"
strptime(t, "%Y-%m-%d %H:%M:%S")
You need to create a second format for the other time string:
t = "2018-07-16 10:17:53.460035"
strptime(t, "%Y-%m-%d %H:%M:%S.%f")
Edit: Here is another example which excepts both
time_stamps = ["2018-05-20 14:37:21", "2018-07-16 10:17:53.460035"]
for stamp in time_stamps:
fmt = "%Y-%m-%d %H:%M:%S"
try:
time = datetime.datetime.strptime(stamp, fmt+".%f")
except ValueError:
time = datetime.datetime.strptime(stamp, fmt)
print(time)

Converting string to date that contains 00:00:00

To convert a string date to date format dropping the '00:00:00' I use :
import datetime
strDate = '2017-04-17 00:00:00'
datetime.datetime.strptime(strDate, '%Y/%m/%d %H:%M:%S').strftime('%Y-%m-%d')
Returns :
ValueError: time data '2017-04-17 00:00:00' does not match format '%Y/%m/%d %H:%M:%S'
Is %H:%M:%S not correct format ?
This is the correct way:
datetime.datetime.strptime(strDate, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
Notice the - instead of / in strptime. The date is converted to: 2017-04-17.
If you would like to have it displayed a different way, have a look here.

Django date YYYY-MM-DD

I get date in format(YYYY-MM-DD)
Then I want to add timedelta
mydate + timedelta(days=1)
and I get error
coercing to Unicode: need string or buffer, datetime.timedelta found
mydate is a string. Try doing this:
from datetime import datetime
parsed_date = datetime.strptime(mydate, "%Y-%m-%d")
new_date = parsed_date + timedelta(days=1)
Data sent by the client will be sent as a Unicode and you have to parse it on server side
datetime.strptime(date, '%Y-%m-%d')
If it's part of a form, the data should be reformatted automatically when cleaned (though you might need to configure the field to accept the format you expect).
You have to convert your string date to python datetime:
>>> from datetime import datetime, timedelta
>>> dt_str = "2013/10/11"
>>> dt = datetime.strptime(dt_str, "%Y/%m/%d")
>>> new_dt = dt + timedelta(days=1)
>>> print new_dt
datetime.datetime(2013, 10, 12, 0, 0)
If you now want to get the date as string:
>>> print new_dt.strftime("%Y/%m/%d")
'2013/10/12'

Categories