Converting string into Date and Time Values - python

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.'

Related

how to convert ISO 8601 to Date time(Utc) in 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'))

Python get valid date format from date string

Is there a way to guess datetime format of a given string in python?
e.g. desired usage would be:
> guess_format('2020-02-24 07:22')
'%Y-%m-%d %H:%M'
There's dateutil project which automates datetime string conversion to valid Datetime objects:
> from dateutil.parser import parse
> parse('2020-02-24 07:22')
datetime.datetime(2020, 2, 24, 7, 22)
but can it produce valid formatting strings?
The pydateinfer package provides the possibility to infer the datetime format string of a given date string.
Example:
>>> import dateinfer
>>> dateinfer.infer(['Mon Jan 13 09:52:52 MST 2014', 'Tue Jan 21 15:30:00 EST 2014'])
'%a %b %d %H:%M:%S %Z %Y'

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)

Convert string to DateTime for mysql in python

I want to convert my date into DateTime object for MySQL.
My string format is: Mon Aug 27 04:47:45 +0000 2018
Expected Output: 'YYYY-M-D H:mm:ss'
from datetime import datetime
t = datetime.strptime('Mon Aug 27 04:47:45 +0000 2008', '%a %b %d %H:%M:%S % z %Y')
t.strftime('%Y-%m-%d %H:%M:%S')
Refer section 8.1.8
here
If you are using python 3, this solution would work -
from datetime import datetime
x = 'Mon Aug 27 04:47:45 +0000 2018'
x = datetime.strftime(datetime.strptime(x, '%a %b %d %I:%M:%S %z %Y'), '%Y-%m-%d %H:%M:%S')
# OP '2018-08-27 04:47:45'
But for python 2, you might get a ValueError: 'z' is a bad directive.... In that case, you'll either have to use something like pytz or dateutil. The table that you need to look for all these conversions can be found here
Edit: You can't have Expected Output: 'YYYY-M-D H:mm:ss' if you convert your datetime string to datetime object. Datetime object has it's own format. Above gives you a string of the format that you want
from datetime import datetime
date_as_dt_object = datetime.strptime(dt, '%a %b %d %H:%M:%S %z %Y')
You can use date_as_dt_object in a raw query or an ORM. If used in a raw query pass it as a string like:
query = "select * from table where date >" + str(date_as_dt_object)
Check out this list for Python's strftime directives.
http://strftime.org/

get hours from full date date to python3

I work with api in python3 in this api return date like this
'Jun 29, 2018 12:44:14 AM'
but i need just hours, minute ad second like this
12:44:14
are there a fonction that can format this
It looks like the output is a string. So, you can use string slicing:
x = 'Jun 29, 2018 12:44:18 AM'
time = x[-11:-3]
It's best to use negative indexing here because the day may be single-digit or double-digit, so a solution like time = x[13:21] won't work every time.
If you're inclined, you may wish to use strptime() and strftime() to take your string, convert it into a datetime object, and then convert that into a string in HH:MM:SS format. (You may wish to consult the datetime module documentation for this approach).
Use the datetime module. Use .strptime() to convert string to datetime object and then .strftime() to convert to your required string output. Your sample datetime string is represented as '%b %d, %Y %H:%M:%S %p'
Ex:
import datetime
s = 'Jun 29, 2018 12:44:14 AM'
print( datetime.datetime.strptime(s, '%b %d, %Y %H:%M:%S %p').strftime("%H:%M:%S") )
Output:
12:44:14

Categories

Resources