Converting month in number to word [duplicate] - python

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 4 years ago.
i have string
date = "2018-09-12"
i want to get output like 2018-September-12
and i try like this
from datetime import datetime
date3 = datetime.strptime(date, '%Y-%m%B-%d')
or date3 = datetime.strptime(date, '%Y-%B-%d')
but always get time data '2018-09-12' does not match format '%Y-%m%B-%d'

Use strftime
Ex:
from datetime import datetime
date = "2018-09-12"
date3 = datetime.strptime(date, '%Y-%m-%d').strftime("%Y-%B-%d")
print(date3)
Output:
2018-September-12
strptime to convert string datetime to datetime object.
strftime to convert datetime object to required string format.

Related

Convert YYYY-MM-DD to DD-MMM-YYYY in Python [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 24 days ago.
I'm trying to convert a list of dates (strings) in the format 2023-01-19 into the format 19-Jan-2023. The code I currently have does not work:
date_list = ['2023-01-19', '2023-01-07', '2022-11-29']
new_date_list = []
for date in date_list:
date_new_format = datetime.datetime(date, '%dd-%mmm-%yyyy')
new_date_list.append(date_new_format)
You have to first create a datetime object with strptime, then you can use strftime to reformat it:
from datetime import datetime
date_list = ['2023-01-19', '2023-01-07', '2022-11-29']
for date in date_list:
d = datetime.strptime(date, "%Y-%m-%d")
date_new_format = datetime.strftime(d, '%d-%b-%Y')
print(date_new_format)

Convert date string format to a datetime Python Object [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 2 years ago.
i've one doubt.
I'm doing request to an API which return me event date. I was hope that date will be a timestamp, but i get this value:
{"date":"2020-08-24T21:15:00+00:00"}
I want to get a python datetime object.
How can I do that?
from datetime import datetime
dates = {"date":"2020-08-24T21:15:00+00:00"}
date = dates.get("date")
day = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S+00:00")
Your looking for strptime.
Heres a good article:
https://www.programiz.com/python-programming/datetime/strptime
Use dateutil.parser which smartly parse date string:
import json
import dateutil.parser
result = '{"date":"2020-08-24T21:15:00+00:00"}'
x = json.loads(result)
dt = dateutil.parser.parse(x['date'])
# 2020-08-24 21:15:00+00:00
print(dt)
# <class 'datetime.datetime'>
print(type(dt))
I think you can do it respecting the format while parsing the string:
You have to try to follow the structure of the string and assign each value to the correct time value. For example:
str = '2018-06-29 08:15:27.243860'
time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
Note that your case is pretty different.
It could be similar to '%Y-%m-%dT%H:%M:%S.f'

How to convert date as string to date type [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have a variable '2019-05-30 21:01:09' that needs to be converted into 2019-05-30 21:01:09. What is the best way to go about this.
from datetime import datetime
strToDate = datetime.strptime('2019-05-30 21:01:09','%Y-%m-%d %H:%M:%S')
strptime() allows for the conversion of string to date time object provided that you give the format the function should expect as the second argument
You can using datetime library
from datetime import datetime
datetime.strptime('2019-05-30 21:01:09', '%Y-%m-%d %H:%M:%S')
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Change time format in Python [duplicate]

This question already has answers here:
How do I translate an ISO 8601 datetime string into a Python datetime object? [duplicate]
(11 answers)
Closed 5 years ago.
I have DateTime String in this format "2018-02-08T23:59:05.823Z" I know 'T ' is separating date from time, I have split the string on 'T' delimiter and I got Time - > '23:59:05.823Z'
I want to convert this time '23:59:05.823Z' to this format like '10:00 AM'
How can we do that in python ?
I want to convert this time '23:59:05.823Z' to this format like '10:00
AM'
looks like you are using ISO date time format. You could do something like this.
>>> from datetime import datetime
>>> d = datetime.strptime("23:59:05.823Z", "%H:%M:%S.%fZ")
>>> d.strftime("%I:%M %p")
'11:59 PM'
If you are using the Python datetime module then you can use the .strftime() method to convert you datetime into a string of your desired format. For example:
from datetime import datetime
current_datetime = datetime.now()
print(current_datetime)
>>> 2018-02-17 09:23:31.079326
print(current_datetime.strftime("%I:%M %p")
>>> 09:23 AM
%I gives you the hour in a 12-hour clock as a zero-padded number, %M gives you the minute as a zero-padded number, and %p gives you AM or PM. You can read more about this in the official documentation: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

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