How can I compare two time strings in the format HH:MM:SS?
Date.parse('01/01/2011 10:20:45') > Date.parse('01/01/2011 5:10:10')
> true
The 1st January is an arbitrary date, doesn't mean anything.
Difference between two time.Time objects
You may use Time.Sub()
to get the difference between the 2 time.Time
values, result will be a value of time.Duration
.
When printed, a time.Duration
formats itself "intelligently":
t1 := time.Now()
t2 := t1.Add(time.Second * 341)
fmt.Println(t1)
fmt.Println(t2)
diff := t2.Sub(t1)
fmt.Println(diff)
Output:
2009-11-10 23:00:00 +0000 UTC
2009-11-10 23:05:41 +0000 UTC
5m41s
If you want the time format HH:mm:ss
, you may constuct a time.Time
value and use its Time.Format()
method like this:
out := time.Time{}.Add(diff)
fmt.Println(out.Format("15:04:05"))
Output:
00:05:41
Try the examples on the Go Playground.
Of course this will only work if the time difference is less than a day. If the difference may be bigger, then it's another story. The result must include days, months and years. Complexity increases significnatly. See this question for details:
golang time.Since() with months and years
The solution presented there solves this issue by showing a function with signature:
func diff(a, b time.Time) (year, month, day, hour, min, sec int)
You may use that even if your times are within 24 hours (in which case year
, month
and day
will be 0
).
Hour difference between two times(HH:MM:SS a)in momentjs
Get Hours
I got the hours by using this code
endTime.diff(startTime, 'hours')
Get Minutes
i got the minutes by using this below code
var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
My Working code is
$scope.UpdateTimeSheet = function (rowEntity) {
if (rowEntity.StartTime.toString().length != 11) {
rowEntity.StartTime = moment(rowEntity.StartTime).format("hh:mm:ss a");
}
if (rowEntity.EndTime.toString().length != 11) {
rowEntity.EndTime = moment(rowEntity.EndTime).format("hh:mm:ss a");
}
var startTime = moment(rowEntity.StartTime, "hh:mm:ss a");
var endTime = moment(rowEntity.EndTime, "hh:mm:ss a");
var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
rowEntity.TotalHours = endTime.diff(startTime, 'hours') + " Hrs and " + mins + " Mns";
}
SSRS calculate timedifference between two times HH:MM:SS
This is untested but...
If you are only checking times that occur on the same day then you could add an arbitrary date before the time.
Something like
=DateDiff("s",
CDate("2020-01-01 " & Fields!starttime.Value) ,
CDate("2020-01-01 " & Fields!endtime.Value)
)
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 the time difference between two datetimes
This approach will work ONLY when the total duration is less than 24 hours:
var now = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";
moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")
// outputs: "00:39:30"
If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.
If you want to get a valid response for durations of 24 hours or greater, then you'll have to do something like this instead:
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
// outputs: "48:39:30"
Note that I'm using the utc time as a shortcut. You could pull out d.minutes()
and d.seconds()
separately, but you would also have to zeropad them.
This is necessary because the ability to format a duration
objection is not currently in moment.js. It has been requested here. However, there is a third-party plugin called moment-duration-format that is specifically for this purpose:
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");
// outputs: "48:39:30"
Swfit- Check whether current time is between two time string
You can set the date formatter defaultDate
for today, parse the date strings, create a DateInterval
with the start and end date and check if it contains now Date()
:
extension Formatter {
static let today: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.defaultDate = Calendar.current.startOfDay(for: Date())
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter
}()
}
func checkIfCurrentTimeIsBetween(startTime: String, endTime: String) -> Bool {
guard let start = Formatter.today.date(from: startTime),
let end = Formatter.today.date(from: endTime) else {
return false
}
return DateInterval(start: start, end: end).contains(Date())
}
let startTime = "10:30 AM"
let endTime = "06:30 PM"
checkIfCurrentTimeIsBetween(startTime: startTime, endTime: endTime) // true
Compare two dates with JavaScript
The Date object will do what you want - construct one for each date, then compare them using the >
, <
, <=
or >=
.
The ==
, !=
, ===
, and !==
operators require you to use date.getTime()
as in
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
to be clear just checking for equality directly with the date objects won't work
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.
For the curious, date.getTime()
documentation:
Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)
Related Topics
How to Know If a Type Is a Specialization of Std::Vector
Call Destructor and Then Constructor (Resetting an Object)
How to Use Std::Imbue to Set the Locale for Std::Wcout
Get Current Username in C++ on Windows
Why Does a Push_Back on an Std::List Change a Reverse Iterator Initialized with Rbegin
How to Get the Executable Name of a Window
Does (W)Ifstream Support Different Encodings
If Temporaries Are Implicitly Non-Modifiable, How Does This Work
How to Securely Disconnect an Asio Ssl Socket
Why "Initializer-String for Array of Chars Is Too Long" Compiles Fine in C & Not in C++
Does Case-Switch Work Like This
Why Does 'Int ;' Compile Fine in C, But Not in C++
C++ Boost: What's the Cause of This Warning
Std::Map Default Value for Build-In Type