Compare Time Using Date Command

How to compare two DateTime strings and return difference in hours? (bash shell)

You could use date command to achieve this. man date will provide you with more details. A bash script could be something on these lines (seems to work fine on Ubuntu 10.04 bash 4.1.5):

#!/bin/bash                                                                                                                                                   

# Date 1
dt1="2011-11-11 11:11:11"
# Compute the seconds since epoch for date 1
t1=$(date --date="$dt1" +%s)

# Date 2 : Current date
dt2=$(date +%Y-%m-%d\ %H:%M:%S)
# Compute the seconds since epoch for date 2
t2=$(date --date="$dt2" +%s)

# Compute the difference in dates in seconds
let "tDiff=$t2-$t1"
# Compute the approximate hour difference
let "hDiff=$tDiff/3600"

echo "Approx hour diff b/w $dt1 & $dt2 = $hDiff"

Hope this helps!

Bash/Shell Compare a command date/time output with current date/time

Getting the time in seconds since Epoch.

startdate=$(ls -lrt --time-style=+"%s" | tail -1 | awk '{ print $6; }')
enddate=$(date +"%s")
echo "Start date $startdate"
echo "End date $enddate"
difference=$(expr $enddate - $startdate)
echo "Seconds difference $difference"

seconds=$((difference%60))
minutes=$((difference/60%60))
hours=$((difference/60/60%24))
days=$((difference/60/60/24))
printf '%02d:' $days
printf '%02d:' $hours
printf '%02d:' $minutes
printf '%02d\n' $seconds

Compare two dates in shell script

You may need to call out to expr, depending on your mystery shell:

d1="2015-03-31" d2="2015-04-01"
if [ "$d1" = "$d2" ]; then
echo "same day"
elif expr "$d1" "<" "$d2" >/dev/null; then
echo "d1 is earlier than d2"
else
echo "d1 is later than d2"
fi
d1 is earlier than d2

The test command (or it's alias [) only implements string equality and inequality operators. When you give the (non-bash) shell this command:

[ "$d1" > "$d2" ]

the > "$d2" part is treated as stdout redirection. A zero-length file named (in this case) "2015-04-01" is created, and the conditional command becomes

[ "$d1" ]

and as the variable is non-empty, that evaluates to a success status.

The file is zero size because the [ command generates no standard output.

Date comparison in Bash

You can compare lexicographically with the conditional construct [[ ]] in this way:

[[ "2014-12-01T21:34:03+02:00" < "2014-12-01T21:35:03+02:00" ]]

From the man:

[[ expression ]]

Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.


New update:

If you need to compare times with different time-zone, you can first convert those times in this way:

get_date() {
date --utc --date="$1" +"%Y-%m-%d %H:%M:%S"
}

$ get_date "2014-12-01T14:00:00+00:00"
2014-12-01 14:00:00

$ get_date "2014-12-01T12:00:00-05:00"
2014-12-01 17:00:00

$ [[ $(get_date "2014-12-01T14:00:00+00:00") < $(get_date "2014-12-01T12:00:00-05:00") ]] && echo it works
it works

How to compare two dates along with time in java

Since Date implements Comparable<Date>, it is as easy as:

date1.compareTo(date2);

As the Comparable contract stipulates, it will return a negative integer/zero/positive integer if date1 is considered less than/the same as/greater than date2 respectively (ie, before/same/after in this case).

Note that Date has also .after() and .before() methods which will return booleans instead.

Get current date/time and compare with other date

There's not much point in converting datetime.datetime.now() into a string, just so you can convert it right back to a datetime. Just leave it as-is.

import datetime

CurrentDate = datetime.datetime.now()
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
print("Date missed")
else:
print("Date not missed")

Result:

2015-09-09 12:25:00.983745
2015-08-09 04:00:00
Date missed

How to compare times of the day?

You can't compare a specific point in time (such as "right now") against an unfixed, recurring event (8am happens every day).

You can check if now is before or after today's 8am:

>>> import datetime
>>> now = datetime.datetime.now()
>>> today8am = now.replace(hour=8, minute=0, second=0, microsecond=0)
>>> now < today8am
True
>>> now == today8am
False
>>> now > today8am
False

How to compare the date part alone from a date time value

Try clearing the time using Date.setHours:

dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])

Example Code:

var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);

if(d >= today){
alert(d is greater than or equal to current date);
}


Related Topics



Leave a reply



Submit