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.
Related
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
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.
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
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).
This question already has answers here:
Natural language time parser [closed]
(2 answers)
Closed 9 years ago.
How do I convert these strings:
one hour ago
three days ago
two weeks ago
yesterday
next month
into Python datetime object?
Just found parsedatetime for parsing human readable date/time text from the link provided by Jon Clements. Here is a solution in case you interested:
from time import mktime
from datetime import datetime
import parsedatetime as pdt
time_str = '1 hour ago'
cal = pdt.Calendar()
dt = datetime.fromtimestamp(mktime(cal.parse(time_str)[0]))
time_formatted = dt.strftime('%b %d, %Y %H:%M')
print(time_formatted) # will print something like: Dec 15, 2013 02:10
Also see this question: python 'x days ago' to datetime