How can I change date stamps in python [duplicate] - python

This question already has answers here:
How to Convert Datetime to String in Python?
(3 answers)
How do I turn a python datetime into a string, with readable format date?
(8 answers)
Convert datetime object to a String of date only in Python
(15 answers)
Closed 9 months ago.
how can I convert the date format from
Current format : [day] [month] [day value] [hour]:[minute]:[second] [time zone difference] [year]
to
New format : [year]-[month value]-[day value] [hour]:[minute]:[second]
For example, a current format value:
Tue Feb 04 17:04:01 +0000 2020
should be converted to:
2020-02-04 17:04:01
in python

You can use parser. Then use strftime to format the datetime object
from dateutil import parser
x = 'Tue Feb 04 17:04:01 +0000 2020 '
y = parser.parse(x).strftime('%Y-%m-%d %T')
>>> y
'2020-02-04 17:04:01'
Check out:
http://strftime.net/

Related

How to convert datetime string to datetime object in python [duplicate]

This question already has answers here:
How to convert local time string to UTC?
(25 answers)
datetime from string in Python, best-guessing string format
(4 answers)
Closed 11 months ago.
I am converting a datetime object to string using:
from datetime import datetime
dt = datetime.now()
dt_str = str(dt)
# Now I want to get `dt`(datetime object) back from `dt_str`
How to convert dt_str to dt?
Can anyone help me with this?
Use strptime method. Example:
from datetime import datetime
date_string = "2020-03-14"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
%Y - Year in four digits. Example: 2018, 2019 etc.
%m - Month as a zero-padded decimal number. Example: 01, 02, ..., 12
%d - Represents the day of the month. Example: 01, 02, ..., 31

I need to convert string to date in python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I need to convert string to date type.What should I do?
My string is "04 Oct 2019". I need to convert to date type "04/10/2019".
Note:You can't ignore 0
import calendar
def convert_str_to_date(s):
l = s.split()
l[1] = "{0:02d}".format(list(calendar.month_abbr).index(l[1]))
return "/".join(l)
print(convert_str_to_date("04 Oct 2019"))
It won't ignore 0 as "{0:02d}".format

Converting Datetime string with non-zero padded day [duplicate]

This question already has answers here:
Whats wrong with my datetime.strptime format?
(1 answer)
datetime.strptime strange behavior [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to convert a string to datetime object in python. Some examples of the string includes "Fri Oct 20 2006 4:25pm EDT" and "Wed Nov 1 2006 4:47pm EST" The problem seems to be that the day is not zero-padded which is why datetime does not pick up the format
I have also tried slicing it to convert the first 15 characters only but it seems that the non-zero padded dates are still a problem.
import datetime
for i, row in df.iterrows():
datetime_list.append(datetime.datetime.strptime(row['Time Stamp'],
'%a %b %d %Y %I:%M%p %Z'))
I hope to get a datetime object out that only shows the year, month and date.

How to parse the date "Thu, 1 Oct 2015 16:05:43 +0200" in python? [duplicate]

This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
How to remove unconverted data from a Python datetime object
(5 answers)
Closed 7 years ago.
In python I am trying to parse the date string
Thu, 1 Oct 2015 16:05:43 +0200
to a struct_time trying
x = time.strptime(date_string, "%a, %d %b %Y %H:%M:%S +0000")
which yields a ValueError.
How to avoid the "+0200" (which I am not interested in)? Just remove the last 6 characters? Or is there a 'better' solution?
Just remove the last chunk; you can do that by slicing (removing the last 6 characters, yes), or by partitioning (splitting off the last section):
x = time.strptime(date_string.rpartition(' ')[0], "%a, %d %b %Y %H:%M:%S")
Partitioning should continue to work if the last chunk is not 6 characters (say, because you have dates with the timezone expressed as a 3-letter code).

How to convert string to timestamp? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert Date String to DateTime Object in Python
I have a date string:
Mon Oct 15 15:05:00 UTC 2012
How to converte this string to timestamp ?
This should help:
http://docs.python.org/library/time.html#time.strptime

Categories