how to convert string datetime to utc format in python? - python

I have a variable datem -
datem = '2023-02-13T15:34:11.358472'
How to convert the above string datem to utc format?

Related

How to convert the string to date time format?

I have the following string 20211208_104755, representing date_time format. I want to convert it to the python datetime format using datetime.strip() method.
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%y/%m/%d')
However I am getting the following error.
ValueError: time data '20211208' does not match format '%y/%m/%d'
The second argument in strptime has to match the pattern of your datetime string. You can find the patterns and their meaning on https://docs.python.org/3/library/datetime.html
In your case, you can format it as
from datetime import datetime
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print(datetime_object)
>>> 2021-12-08 10:47:55
what should work is to define how the mydatetime string is composed.
example:
%Y is the year (4 digits); check here for format (section strftime() Date Format Codes)
So in your example I would assume it's like this:
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print (datetime_object)
result
2021-12-08 10:47:55
and
type(datetime_object)
datetime.datetime

How do I convert date and time string(1975-07-14T16:32:47.000Z) to Day-Month name-Year (14 July 1975) 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 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

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

How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python

How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python
OR, I have a date = 2020-06-11 which I want to convert into seconds.
Works: d2 = (dt.datetime(2020,6,30) - t0).total_seconds()
Fails: d2 = (dt.datetime(2020-6-30) - t0).total_seconds()
How do I do it ?
If you need to cast a string to a datetime object, you can use strptime for this.
from datetime import datetime
t = datetime.strptime('2020-06-11', '%Y,%m,%d')
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

string convert to date time in python

I have a list of date time strings like this.
16-Aug-2019
I want to convert the string to 2019-08-01 this date format, and I have tried on this code , but it's getting me an error.
formatd_date = datetime.strptime(formatd_date, '%y-%m-%d')
ValueError: time data 'As-of' does not match format '%y-%m-%d'
If any can help, it will be huge thank.
Convert to datetime format and then convert to string format you want to:
>>> from datetime import datetime
>>> a = "16-Aug-2019"
>>> datetime.strptime(a, "%d-%b-%Y").strftime("%Y-%m-%d")
'2019-08-16'
Documentation: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Just fails because %y is 2-digit year. Use %Y for 4-digit year.

Categories

Resources