@true-directive/base
Version:
The set of base classes for the TrueDirective Grid
174 lines (173 loc) • 6.96 kB
JavaScript
/**
* Copyright (c) 2018-2019 Aleksey Melnikov, True Directive Company.
* @link https://truedirective.com/
* @license MIT
*/
var Dates = /** @class */ (function () {
function Dates() {
}
Dates.isEmpty = function (d) {
return d === null || d === undefined || d.getTime === undefined || d.getTime() === NaN;
};
/**
* Returns timestamp of the given date or null
* @param d Date
* @return Timestamp or date's timestamp
*/
Dates.getTimeNull = function (d) {
if (d === null || d === undefined) {
return null;
}
return d.getTime();
};
/**
* Are two dates equal?
* @param d1 First date
* @param d2 Second date
* @return True if dates are equals.
*/
Dates.equals = function (d1, d2) {
if (!d1 && !d2) {
return true;
}
if (!d1 || !d2) {
return false;
}
return d1.getTime() === d2.getTime();
};
/**
* Comparing two dates.
* @param d1 First date.
* @param d2 Second date.
* @return True if both dates are equal.
*/
Dates.compare = function (d1, d2) {
if (!d1 && !d2) {
return 0;
}
if (!d1 && d2) {
return -1;
}
if (d1 && !d2) {
return 1;
}
if (d1.getTime() === d2.getTime()) {
return 0;
}
return d1.getTime() > d2.getTime() ? 1 : -1;
};
/**
* Returns today's date with no time.
* @return Today's date with no time.
*/
Dates.today = function () {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
};
/**
* Returns yesterday's date without time.
* @return Yesterday's date without time.
*/
Dates.yesterday = function () {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
};
/**
* Returns the date following the specified date.
* @param d Date
* @return The date following the date.
*/
Dates.nextDate = function (d) {
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1);
};
/**
* Returns a date that is more than the specified date for the specified number of days.
* @param d Date
* @param days Number of days
* @return
*/
Dates.addDays = function (d, days) {
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + days);
};
// Первая дата недели, включающей заданную дату
Dates.firstDateOfWeek = function (d, firstDay) {
var day = d.getDay();
if (firstDay > day) {
day += 7;
}
day -= firstDay;
return new Date(d.getFullYear(), d.getMonth(), d.getDate() - day);
};
// Последняя дата недели, включающей заданную дату
Dates.lastDateOfWeek = function (d, firstDay) {
var firstDate = Dates.firstDateOfWeek(d, firstDay);
return new Date(firstDate.getFullYear(), firstDate.getMonth(), firstDate.getDate() + 6);
};
// Первая дата недели, которая следует за неделей, включающей заданную дату
Dates.lastDateOfPrevWeek = function (d, firstDay) {
var firstDate = Dates.firstDateOfWeek(d, firstDay);
return new Date(firstDate.getFullYear(), firstDate.getMonth(), firstDate.getDate() - 1);
};
// Первая дата недели, которая следует за неделей, включающей заданную дату
Dates.firstDateOfNextWeek = function (d, firstDay) {
var lastDate = Dates.lastDateOfWeek(d, firstDay);
return new Date(lastDate.getFullYear(), lastDate.getMonth(), lastDate.getDate() + 1);
};
// Первая дата месяца, включающего заданную дату
Dates.firstDateOfMonth = function (d) {
return new Date(d.getFullYear(), d.getMonth(), 1);
};
// Последная дата месяца, включающего заданную дату
Dates.lastDateOfMonth = function (d) {
return new Date(d.getFullYear(), d.getMonth() + 1, 0);
};
// Первая дата месяца, который следует за месяцем, включающим заданную дату
Dates.firstDateOfNextMonth = function (d) {
var last = Dates.lastDateOfMonth(d);
return new Date(last.getFullYear(), last.getMonth(), last.getDate() + 1);
};
// Последняя дата месяца, который следует за месяцем, включающим заданную дату
Dates.lastDateOfNextMonth = function (d) {
return Dates.lastDateOfMonth(Dates.firstDateOfNextMonth(d));
};
// Первая дата месяца, который предшествует месяцу, включающему заданную дату
Dates.firstDateOfPrevMonth = function (d) {
var last = Dates.lastDateOfPrevMonth(d);
return Dates.firstDateOfMonth(last);
};
// Последняя дата месяца, который предшествует месяцу, включающему заданную дату
Dates.lastDateOfPrevMonth = function (d) {
var first = Dates.firstDateOfMonth(d);
return new Date(first.getFullYear(), first.getMonth(), first.getDate() - 1);
};
// Дата находится в интервале между двумя заданными датами (включительно)
Dates.dateBetween = function (d, start, end) {
return d.getTime() >= start.getTime() && d.getTime() <= end.getTime();
};
// Дата находится в том же месяце, что и вторая дата
Dates.isSameMonth = function (d1, d2) {
if (d1 === null || d1 === undefined) {
return false;
}
if (d2 === null || d2 === undefined) {
return false;
}
return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth();
};
// Дата находится в том же году, что и вторая дата
Dates.isSameYear = function (d1, d2) {
if (d1 === null || d1 === undefined) {
return false;
}
if (d2 === null || d2 === undefined) {
return false;
}
return d1.getFullYear() === d2.getFullYear();
};
Dates.yearTwoDigits = function (d) {
var yy = d.getFullYear();
return (yy + '').substring((yy + '').length - 2);
};
return Dates;
}());
export { Dates };