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

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

Related

Why is pytz US/Eastern -0456 and not -0500 [duplicate]

This question already has answers here:
Python pytz timezone function returns a timezone that is off by 9 minutes
(4 answers)
Weird timezone issue with pytz
(3 answers)
Closed 3 months ago.
Working with pytz in Python. I have a datetime objects that I am trying to convert to different timezones. When printing the UTC Offset, I am not getting a -0500. What's the reason for this?
original_utc = datetime.datetime(1900,6,6,10,40, tzinfo=pytz.timezone('UTC'))
in_eastern = lesson.astimezone(pytz.timezone('America/New_York'))
print("UTC time: ", original_utc.strftime("%Y:%m:%d %H:%M:%S %Z %z"))
print("----------")
print("EST time", in_eastern.strftime("%Y:%m:%d %H:%M:%S %Z %z"))
Gives me:
UTC time: 1900:06:06 10:40:00 UTC +0000
----------
EST time 1900:06:06 05:44:00 LMT -0456

How do I turn variable into converted time/date? [duplicate]

This question already has answers here:
How to convert milliseconds to date and time format?
(2 answers)
Closed 4 months ago.
I have a variable named "timestamp" and the value of it is "1617108899460", how would I turn this into this format: month/day/year, hour:minutes:seconds
I tried to do the following but I got an error:
date_time = datetime.fromtimestamp(timestamp).strftime("%A, %B %d, %Y %I:%M:%S")
OSError: [Errno 22] Invalid argument
Is there any way I can do this in a simple way? Like with a module?
Duplicate of this. Your timestamp value is in milliseconds.
from datetime import datetime
timestamp = 1617108899460
date_time = datetime.fromtimestamp(timestamp / 1e3).strftime("%A, %B %d, %Y %I:%M:%S")
This works as expected.

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.

Handling of timezones in strptime in Python [duplicate]

This question already has answers here:
How to parse dates with -0400 timezone string in Python?
(6 answers)
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have two string like these:
"Sun 10 May 2015 13:54:36 -0700"
"Sun 10 May 2015 13:54:36 +0000"
(The last part is time zone -0700 means 7 hours less than GMT)
My intention is turn it to a unix timestamp to find their absolute difference easily.
My Question is is there a built-in method for turning these formated texts to Unix timestamp? Or Is there a one that computes difference of two such strings directly??
Edit:
This Question differs from This one since that problem doesn't include time zones and not any of answers to that question mentioned anything about time zone.
This should work on Python 3 & above but not known to work on all platforms on Python 2.
date_str = "Sun 10 May 2015 13:54:36 -0700"
pattern = '%a %d %B %Y %H:%M:%S %z'
dt = int(time.mktime(time.strptime(date_str ,pattern )))
print(dt)
For, Python 2.7+, (without %z)
import datetime
date_str = "Sun 10 May 2015 13:54:36 -0700"
dt_str = date_str[:-5].strip()
dt_zone = int(date_str[-5:])/100
pattern = '%a %d %B %Y %H:%M:%S'
dtobj = datetime.datetime.strptime(dt_str ,pattern)
dt = dtobj + datetime.timedelta(hours=dt_zone)
print( dt.strftime('%s') )
This code answer to your question:
from datetime import timedelta, datetime, tzinfo
date1="Sun 10 May 2015 13:54:36 -0700"
date2=date1[4:-6]
zone = int(date1[-5:])/100
d=datetime.strptime(date2, "%d %B %Y %H:%M:%S")
class TZ(tzinfo):
def utcoffset(self, dt): return timedelta(hours=zone)
d=datetime(d.year, d.month, d.day,d.hour,d.minute,d.second, tzinfo=TZ()).isoformat(' ')
print d

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