2025-09-06 11:33:04 +00:00
|
|
|
import { leadingZeros } from "../text/leadingZeros";
|
2025-03-25 06:42:27 +00:00
|
|
|
import { DateHelper } from "./DateHelper";
|
|
|
|
|
|
|
|
export class DateMath
|
|
|
|
{
|
2025-09-06 11:33:04 +00:00
|
|
|
static getMinutesAndSeconds( allSeconds:number )
|
|
|
|
{
|
|
|
|
let minutes = Math.floor( allSeconds / 60 );
|
|
|
|
let seconds =Math.round( allSeconds - minutes * 60 );
|
|
|
|
|
|
|
|
return { minutes, seconds };
|
|
|
|
}
|
|
|
|
|
|
|
|
static formatToMinutesAndSeconds( allSeconds:number )
|
|
|
|
{
|
|
|
|
let values = this.getMinutesAndSeconds( allSeconds );
|
|
|
|
let minutes = isNaN( values.minutes ) ? "--" : leadingZeros( values.minutes, 2 );
|
|
|
|
let seconds = isNaN( values.seconds ) ? "--" : leadingZeros( values.seconds, 2 );
|
|
|
|
return `${minutes}:${seconds}`;
|
|
|
|
}
|
|
|
|
|
2025-03-25 06:42:27 +00:00
|
|
|
static sort( dates:Date[] )
|
|
|
|
{
|
|
|
|
dates.sort( DateMath.sortDate );
|
|
|
|
}
|
|
|
|
|
|
|
|
static sortDate( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return DateMath.isBefore( a, b ) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isNowOlderThanMS( d:Date, ms:number )
|
|
|
|
{
|
|
|
|
let difference = this.getDifferenceMs( DateHelper.now(), d );
|
|
|
|
return difference > ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isBeforeNow( d:Date )
|
|
|
|
{
|
|
|
|
return d.getTime() < DateHelper.now().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isInThePast( d:Date )
|
|
|
|
{
|
|
|
|
return DateMath.isBeforeNow( d );
|
|
|
|
}
|
|
|
|
|
|
|
|
static isExpired( d:Date )
|
|
|
|
{
|
|
|
|
return DateMath.isInThePast( d );
|
|
|
|
}
|
|
|
|
|
|
|
|
static isExpiredMS( dateMS:number, durationMS:number )
|
|
|
|
{
|
|
|
|
let end = dateMS + durationMS;
|
|
|
|
|
|
|
|
return end <= DateHelper.now().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isBefore( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return a.getTime() < b.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isBeforeOrEquals( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return a.getTime() <= b.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isAfter( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return a.getTime() > b.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isAfterNow( d:Date )
|
|
|
|
{
|
|
|
|
return d.getTime() > DateHelper.now().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isInTheFuture( d:Date )
|
|
|
|
{
|
|
|
|
return DateMath.isAfterNow( d );
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDifferenceMs( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return a.getTime() - b.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static isAfterOrEquals( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
return a.getTime() >= b.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static addMilliseconds( a:Date, durationMS:number )
|
|
|
|
{
|
|
|
|
let dateMS = a.getTime();
|
|
|
|
let milliseconds = dateMS + durationMS;
|
|
|
|
|
|
|
|
return new Date( milliseconds );
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromNowAddDays( days:number )
|
|
|
|
{
|
|
|
|
return DateMath.addDays( new Date(), days );
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromNowAddHours( hours:number )
|
|
|
|
{
|
|
|
|
return DateMath.addHours( new Date(), hours );
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromNowAddMinutes( minutes:number )
|
|
|
|
{
|
|
|
|
return DateMath.addMinutes( new Date(), minutes );
|
|
|
|
}
|
|
|
|
|
|
|
|
static addSeconds( a:Date, duration:number )
|
|
|
|
{
|
|
|
|
return this.addMilliseconds( a, duration * 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static addMinutes( a:Date, durationMinutes:number )
|
|
|
|
{
|
|
|
|
return this.addSeconds( a, durationMinutes * 60 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static addHours( a:Date, durationHours:number )
|
|
|
|
{
|
|
|
|
return this.addMinutes( a, durationHours * 60 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static addDays( a:Date, durationDays:number )
|
|
|
|
{
|
|
|
|
return this.addHours( a, durationDays * 24 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static nextMonth( date:Date )
|
|
|
|
{
|
|
|
|
date = new Date( date );
|
|
|
|
|
|
|
|
if ( date.getMonth() == 11 )
|
|
|
|
{
|
|
|
|
date.setFullYear( date.getFullYear() + 1, 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
date.setMonth( date.getMonth() + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isOnSameDay( a:Date, b:Date )
|
|
|
|
{
|
|
|
|
if ( a.getFullYear() !== b.getFullYear() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( a.getMonth() !== b.getMonth() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.getDate() === b.getDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|