Find If 24 Hrs Have Passed Between Datetimes

Find if 24 hrs have passed between datetimes

If last_updated is a naive datetime object representing the time in UTC:

from datetime import datetime, timedelta

if (datetime.utcnow() - last_updated) > timedelta(hours=24):
# more than 24 hours passed

If last_updated is the local time (naive (timezone-unaware) datetime object):

import time

DAY = 86400
now = time.time()
then = time.mktime(last_updated.timetuple())
if (now - then) > DAY:
# more than 24 hours passed

If last_updated is an ambiguous time e.g., the time during an end-of-DST transition (once a year in many timezones) then there is a fifty-fifty chance that mktime() returns a wrong result (e.g., off by an hour).

time.mktime() may also fail if C time library doesn't use a historical timezone database on a given platform and the UTC offset for the local timezone was different at last_updated time compared to now. It may apply to more than a third of all timezones in the last year. Linux, OS X, the recent versions of Windows have the tz database (I don't know whether old Windows versions would work for such past dates).

Beware: it might be tempting to write datetime.now() - last_updated (similar to the UTC case) but it is guaranteed to fail on all platforms if the UTC offset was different at last_updated time (it is possible in many timezones). mktime()-based solution can utilize the tz database at least on some platforms and therefore it can handle the changes in the UTC offset for whatever reason there.

For portability, you could install the tz database. It is provided by pytz module in Python. tzlocal can return pytz timezone corresponding to the local timezone:

from datetime import datetime, timedelta
from tzlocal import get_localzone # $ pip install tzlocal

tz = get_localzone() # local timezone
then = tz.normalize(tz.localize(last_updated)) # make it timezone-aware
now = datetime.now(tz) # timezone-aware current time in the local timezone
if (now - then) > timedelta(hours=24):
# more than 24 hours passed

It works even if the UTC offset was different in the past. But it can't (as well as time.mktime()) fix ambiguous times (tz.localize() picks is_dst=False time by default). tz.normalize() is called to adjust non-existing times e.g., those that correspond to a start-of-DST transition (it should not affect the result).

The above code assumes that last_updated is a naive datetime object (no associated timezone info). If last_updated is an aware datetime object then it is easy to convert it to UTC:

from datetime import datetime, timedelta

then_in_utc = last_updated.replace(tzinfo=None) - last_updated.utcoffset()
if (datetime.utcnow() - then_in_utc) > timedelta(hours=24):
# more than 24 hours passed

General note: you should understand now why people recommend to work with UTC time and to use local time only for display.

Python check if date is within 24 hours

Like that?

if now-timedelta(hours=24) <= set_date <= now:
... #date less than 24 hours in the past

If you want to check for the date to be within 24 hours on either side:

if now-timedelta(hours=24) <= set_date <= now+timedelta(hours=24):
... #date within 24 hours

Check if a date is 24 hours old

Add this const oneday = 60 * 60 * 24 * 1000 (milliseconds). Since, JS converts difference between 2 Datetime objects into milliseconds. I think that should work

PHP > Check if a whole 24 hours have passed with time();

$timefromdatabase = 1489834968;

$dif = time() - $timefromdatabase;

if($dif > 86400)
{
echo 'more than 24 hours';
}else{
echo 'less than 24 hours';
}

How do I find the time difference between two datetime objects in python?

>>> import datetime
>>> first_time = datetime.datetime.now()
>>> later_time = datetime.datetime.now()
>>> difference = later_time - first_time
datetime.timedelta(0, 8, 562000)
>>> seconds_in_day = 24 * 60 * 60
>>> divmod(difference.days * seconds_in_day + difference.seconds, 60)
(0, 8) # 0 minutes, 8 seconds

Subtracting the later time from the first time difference = later_time - first_time creates a datetime object that only holds the difference.
In the example above it is 0 minutes, 8 seconds and 562000 microseconds.

Trying to subtract two dates and get a result in hours(greater than 24 hours time period) in python

This program prints the difference between two times in hours.

from datetime import datetime

now = datetime.now()

# Problem: no year specified!
b = datetime.strptime('Apr 24 11:24', '%b %d %H:%M')

# Fix: Use the current year
b = b.replace(year=now.year)

# Goal: print number of hours between the two dates
diff_in_hours = (now - b).total_seconds() / 3600
print '{} - {} = {} hours'.format(now, b, diff_in_hours)

Javascript check if date fits in last 24 hours

Use the Date object to do what you want - construct a Date object for each date, then compare them using the >, <, <= or >=.

Use of comparison ( ==, !=, ===, and !== ) requires you use date.getTime() as in:

var date1 = new Date();var date2 = new Date(date1);
var same = date1.getTime() === date2.getTime();var notSame = date1.getTime() !== date2.getTime();console.log(same,notSame);

check if a date falls in the next 24 hours with c #

You can use AddHours:

var myDateTime = ...
var now = DateTime.Now;

if (myDateTime <= now.AddHours(24)
&& myDateTime >= now.AddHours(-24))
{

}

Also keep in mind that DateTime is immutable and they cannot be modified after creation. That is why AddHours returns a new instance.

Update:
After seeing M.kazem's answer I have though that you can also use that:

Math.Abs(myDateTime.Subtract(DateTime.Now).TotalHours) <= 24

How to check if 24 hours have passed in Swift

You can use UserDefault to save the date upon creation of the record. The syntax will be

UserDefaults.standard.set(Date(), forKey:"creationTime")

Whenever you want to check the saved date, retrieve it in this way

if let date = UserDefaults.standard.object(forKey: "creationTime") as? Date {
if let diff = Calendar.current.dateComponents([.hour], from: date, to: Date()).hour, diff > 24 {
//do something
}
}


Related Topics



Leave a reply



Submit