I have two dates in JavaScript, start
and finish
, parsed by Moment.js. What would be the most efficient way to swap them if start
is posterior to finish
without having to create a third date? Typically, the dates would be parameters of a function like this one:
function getDates(start, finish) {
var start_date = moment(new Date(start));
var finish_date = moment(new Date(finish));
if (start_date.diff(finish_date) > 0) {
// Swap code goes here...
}
}
The reason why one might want to swap dates is because some functions are defined in such a way that the same result is returned even if the dates are swapped, yet the function needs to know which date is the earlier one. For example, YEARFRAC in Microsoft Excel works that way. You can see my implementation of that function here for an example of a sub-optimal date swapping.
Thanks in advance for your help!