how to convert ISO 8601 to Date time(Utc) in Python - python

I have time stamp in ISO format "2022-06-19T00:00:00+00:00"
and want to convert it in to Date time(utc) format "Jun 19, 2022"

This can be done using the built-in datetime module:
import datetime as dt
input_string = '2022-06-19T00:00:00+00:00'
date = dt.datetime.fromisoformat(input_string)
print(date.strftime('%b %d, %Y'))

Related

How to convert Path("/home/user/mypic.jpg").stat().st_ctime to human-readable datetime format? [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 6 days ago.
I got the creation time of a file using this command:
ctime = Path("/home/user/mypic.jpg").stat().st_ctime
How do I convert this info to a datetime.datatime object that is human-readable? I tried this answer but it failed:
from datetime import datetime
datetime.strptime(str(ctime), "%a %b %d %H:%M:%S %Y")
.st_ctime returns epoch seconds, so first convert it to a datetime object with fromtimestamp, and then use strftime to convert to a string
from pathlib import Path
from datetime import datetime
ctime = Path("...").stat().st_ctime
dt = datetime.fromtimestamp(ctime) # convert from epoch timestamp to datetime
dt_str = datetime.strftime(dt, "%a %b %d %H:%M:%S %Y") # format the datetime
print(dt_str)
strptime is the opposite, it takes a formatted string and converts it back to a datetime object
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> dt
datetime.datetime(2006, 11, 21, 16, 30)

How can I change this format to be datetime format? (python)

I have a dataset which contains some columns with datetime. My problem is, I found this type datetime format:
Apr'11
Apr-11
Apr 11
How can I automatically change this format to be datetime format?
for you can use datetime module
from datetime import datetime
this is the link if you want any confusion
date_string = "Apr'11"
date = datetime.strptime(date_string, "%b'%d")
%b = Locale’s abbreviated month name. (like Apr, Mar, Jan, etc)
%d = Day of the month as a decimal number [01,31]
date_string_2 = "Apr-11"
date = datetime.strptime(date_string, "%b-%d")
date_string_3 = "Apr 11"
date = datetime.strptime(date_string, "%b %d")
You should write this "%b %d" same as like date_string otherwise it will give you, even if you give an extra space.
go to this link to learn more about this:
https://docs.python.org/3/library/time.html

Converting string into Date and Time Values

Please, How do I converts strings like this:
2021-01-15 14:22:56.692234+00:00 into datetime value as this :
Jan. 15, 2021, 3:07 p.m.
You can use datetime module to do the parsing.
from datetime import datetime
datetime.strptime('2019-01-04T16:41:24+0200', "%Y-%m-%dT%H:%M:%S%z")
Use the datetime module, specifically, strptime to convert a string into a datetime object, then strftime to print it out in your desired format.
from datetime import datetime
parsed = datetime.strptime(
'2021-01-15 14:22:56.692234+00:00',
'%Y-%m-%d %H:%M:%S.%f%z'
)
formatted = parsed.strftime('%b. %d, %Y, %-I:%M %p')
print(formatted)
This will give:
'Jan. 15, 2021, 2:22 PM'
There are no way to reproduce exactly p.m. but to replace it manually:
formatted = formatted.replace('AM', 'a.m.').replace('PM', 'p.m.')
print(formatted)
Which gives:
'Jan. 15, 2021, 2:22 p.m.'

how to convert date to mysql format in python

How to convert date to mysql date format like Dec 21, 2019 to 2019-12-21 in python.
Please help, I am new to python.
i tried date.strftime("%Y-%m-%d %H:%M:%S") but its won't work.
You need to provide the correct format string.
import datetime
d = datetime.datetime.strptime("Dec 21, 2019","%b %d, %Y")
d.strftime("%Y-%m-%d")
If you're not sure about the format of date and/or expecting multiple formats, you can use the below code snippet
from dateutil.parser import parse
d = parse("Dec 21, 2019")
d.strftime("%Y-%m-%d")
Example snippet for date conversion.
from datetime import datetime
oldformat = 'Dec 21, 2019'
datetimeobject = datetime.strptime(oldformat,'%b %d, %Y')
newformat = datetimeobject.strftime('%Y-%m-%d')
print(newformat)

How to fix date formatting using python3

I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.

Categories

Resources