How to change format from str to datetime in python [duplicate] - python

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 1 year ago.
I have a string like this:
'2021-07-24'
I want to change this to datetime type like this:
2021-07-24T00:00:00.000+09:00
How to replace str with datetime like that format?

You can try this:
from datetime import datetime, timezone, timedelta
dt_obj = datetime.fromisoformat('2021-07-24').replace(tzinfo=timezone(timedelta(hours=9))).isoformat()
print(dt_obj)
Output:
2021-07-24T00:00:00+09:00

Related

Convert number string to weekday [duplicate]

This question already has answers here:
Converting date/time in YYYYMMDD/HHMMSS format to Python datetime
(3 answers)
How do I get the day of week given a date?
(30 answers)
Closed last month.
How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime() and strptime() Format Codes for documentation of the % strings.
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.
You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday

python convert string to date time [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parse date string and change format
(10 answers)
Closed 2 years ago.
I have a string like this: Monday April 27 2020, 5:14pm
And I want to convert it to date time in python to become: 24-Apr-2020 5:14pm
Would python be able to do that?
from datetime import datetime
str_time = "Monday April 27 2020, 5:14pm"
formatted_time = datetime.strptime(a,"%A %B %d %Y, %I:%M%p")
new_str_representation = formatted_time.strftime("%d-%a-%Y %I:%M%p")
Converting string into datetime
https://docs.python.org/3/library/datetime.html
You can find more informations here.

int to datetime in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parsing datetime in Python..?
(2 answers)
Closed 4 years ago.
I'm receiving data from the port.
This data has date and time.But not formatted.
Example:
'02012019', '090253'
I want to save it to the database and I want to convert this format.
02-01-2019 09:02:53
Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.
Anybody have a suggestion?
from datetime import datetime
date_data ='02012019 09:02:53'
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')
# output '02-01-2019 09:02:53'

Parse this string "20160713161212" to datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have datetime a string "20160713161212".
It is a datetime in string with format YYYYMMDDHHIISS
How to parse it to string with format "07/13/2016 16:12:12" in Python?
I think that's what you are looking for:
from datetime import datetime
dt = datetime.strptime('20160713161212', '%Y%m%d%H%M%S')
new_dt = dt.strftime("%Y/%m/%d %H:%M:%S")
For further info you can take a look here

String to DateTime Object in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 9 years ago.
I have date in string:
mystring = "2013-10-25 07:53:07.367857"
Can I convert this string to datetime object?
Yes, you want
from datetime import datetime
datetime.strptime(mystring, "%Y-%m-%d %H:%M:%S.%f")

Categories

Resources