This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10
Related
This question already has answers here:
How do I translate an ISO 8601 datetime string into a Python datetime object? [duplicate]
(11 answers)
Closed 4 months ago.
I am writing a UI automated test that checks a date in the database and returns the date as a string in this format 1975-07-14T16:32:47.000Z and comparing it to the date that is displayed on the webpage but the date on the webpage is in this format Day-Month name-Year (14 July 1975), therefore I need to convert the date return by the database to Day-Month name-Year (14 July 1975) so that I am comparing like for like. How do I change the date string to the format I need
You can use dateutil.parser to parse the string you got from the datebase into a datetime.datetime, which in turn can be formatted using strftime:
import dateutil.parser
input="1975-07-14T16:32:47.000Z"
dt = dateutil.parser.parse(input)
print(dt.strftime("%d %B %Y"))
from datetime import datetime
dt_string = "1975-07-14T16:32:47.000Z"
datetime_object = datetime.strptime(dt_string, "%Y-%m-%dT%H:%M:%S.%fZ")
new_datetime_string = datetime.strftime(datetime_object, "%d-%B-%Y")
print(new_datetime_string)
# prints "14-July-1975"
We are using datetime module where datetime.strptime will generate a datetime object where you can call .date(),.time(),.today() and other functions but to get back to string as per the given format of Day-Month Name-Year datetime.strftime()(stringify time) is used. This converts datetime obj to given format of datetime string.
%d - date (DD - 01,02,...,31)
%m - month (MM - 01,02,...,12)
%Y - Year (YYYY - 2022,2021,...)
%B - Full Month Name (January, Feburary,..)
%f - Milliseconds
you can find out more in following link: Datetime format codes
This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 2 years ago.
How can I get the the import function to convert the date into a European date ? Example: (‘4/7/14’) returns ‘07/04/12’
import datetime
today = datetime.date.today()
print(today)
that is what datetime.strftime is for. have a look at the format codes to get your desired result.
for example:
import datetime
today = datetime.date.today()
print(today.strftime("%Y-%m-%d")) # 2020-12-02
print(today.strftime("%a %d %b %Y")) # Wed 02 Dec 2020
print(today.strftime("%d/%m/%Y")) # 02/12/2020
This question already has answers here:
Generate RFC 3339 timestamp in Python [duplicate]
(7 answers)
Closed 2 years ago.
I would like to convert today's date to below format in python
What I tried:
>>> import datetime
>>> d_date = datetime.datetime.now()
>>> reg_format_date = d_date.strftime("%Y-%m-%d %I:%M:%S %p")
>>> print(reg_format_date)
2020-08-04 06:40:52 PM
Expected format:
2017-10-18T04:46:53.553472514Z
can some one suggest please
Use utcnow() instead of now() to get the UTC time.
Use the isoformat() method. You'll need to add the trailing "Z" yourself.
In summary:
from datetime import datetime
reg_format_date = datetime.utcnow().isoformat() + "Z"
Here's how to get the current UTC time and convert to the ISO-8601 format (which is what your example shows). The timezone is hardcoded to Z.
import datetime
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat() + 'Z'
This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I know this has been asked a few times, but my scenario is a little different... The objective I need to accomplish is to convert a string of digits '20150425' (which happens to be a date), into a date format such as, '2015-04-25'. I need this because I am trying to compare date objects in my code, but have one variable type represented as a string.
Example below:
date = '20150425' ## want to convert this string to date type format
# conversion here
conv_date = '2015-04-25' ## format i want it converted into
Hope this is clear. Should not be difficult, just do not know how to do it.
This works
from datetime import datetime
date = '20150425'
date_object = datetime.strptime(date, '%Y%m%d')
date_object
>>> datetime.datetime(2015,4,25,0,0)
Assuming the date strings will always be 8 characters:
date = '20150425'
fdate = "{}-{}-{}".format(date[0:4], date[4:6], date[6:]) # 2015-04-25
Alternatively, you can go the "heavier" route and use the actual datetime class:
from datetime import datetime
date = '20150425'
dt = datetime.strptime(date, "%Y%m%d")
dt.strftime("%Y-%m-%d") # 2015-04-25