locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
22 lines (21 loc) • 913 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sub = Sub;
const toTimeDate = (value) => {
const date = value instanceof Date ? new Date(value.getTime()) : new Date(value);
if (Number.isNaN(date.getTime())) {
throw new TypeError('Sub(): invalid date input');
}
return date;
};
function Sub(left, right) {
// discuss at: https://locutus.io/golang/time/Sub
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returns the duration in milliseconds between two instants (left - right).
// example 1: Sub('2026-03-03T13:14:15.500Z', '2026-03-03T13:14:15.000Z')
// returns 1: 500
// example 2: Sub('2026-03-03T13:14:10.000Z', '2026-03-03T13:14:15.000Z')
// returns 2: -5000
return toTimeDate(left).getTime() - toTimeDate(right).getTime();
}