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

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.

Related

Convert a number sequence into a date using python? (example: 221201) [duplicate]

This question already has answers here:
How can I convert a string into a date object and get year, month and day separately?
(4 answers)
Closed 2 months ago.
I have a bunch of number sequences like "221201" meaning the year 2022, month 12 and day 01 (so 2022-12-01). How can I convert number sequences in this format into the actual date using python?
I've tried using the dateutil library but couldn't figure out how to get it to recognize this format.
from datetime import datetime
date_str = '221201'
date_obj = datetime.strptime(date_str, '%y%m%d')
print(type(date_obj))
print(date_obj) # printed in default format
You can learn more about strptime in this link

How can I change date stamps in python [duplicate]

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/

Format email Date into "%Y-%m-%d %H:%M:%S" [duplicate]

This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
ISO to datetime object: 'z' is a bad directive [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to format the date of an email using python2.7. The value of date is like
Date: "Tue, 9 Apr 2019 11:49:26 -0400"
Here's the code:
"date": datetime.strptime(d_items['Date'], '%a,%d %b %Y %H:%M:%S %z').strftime('%Y-%m-%d %H:%M:%S')
While stripping the timezone and formatting I'm getting the below error using datetime.datetime function.
'z' is a bad directive in format '%a,%d %b %Y %H:%M:%S %z'
Is there any other way to get the format like below:
2019-04-09 11:49:26

Days between dates [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
How do I parse an HTTP date-string in Python?
(4 answers)
Closed 5 years ago.
I need count days to password change in linux.
I know how to do this in python e.g:
>>> import datetime
>>> start = datetime.date(2016,1,1)
>>> end = datetime.date(2016,2,28)
>>> end-start
But my date format is:
Oct 03, 2017
How I can calculate days to a date?
You can do this using strftime().
It is actually really simple:
from datetime import datetime
d1 = datetime.strptime("Jan 01, 2016", '%b %d, %Y')
d2 = datetime.strptime("Feb 28, 2016", '%b %d, %Y')
print "Delta (in days):", (d2-d1).days
And you'll get 58 as a result.

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

Categories