@barinbritva/add-to-calendar
Version:
[](https://github.com/barinbritva/add-to-calendar/blob/master/package.json) [](https://gith
24 lines (23 loc) • 1.05 kB
JavaScript
export class DateHelper {
static dateToDateString(date) {
const year = date.getFullYear();
const month = DateHelper.addLeadingZeroIfNeeded(date.getMonth() + 1);
const day = DateHelper.addLeadingZeroIfNeeded(date.getDate());
return `${year}-${month}-${day}`;
}
static dateToDateTimeString(date) {
const year = date.getUTCFullYear();
const month = DateHelper.addLeadingZeroIfNeeded(date.getUTCMonth() + 1);
const day = DateHelper.addLeadingZeroIfNeeded(date.getUTCDate());
const hours = DateHelper.addLeadingZeroIfNeeded(date.getUTCHours());
const minutes = DateHelper.addLeadingZeroIfNeeded(date.getUTCMinutes());
const seconds = DateHelper.addLeadingZeroIfNeeded(date.getUTCSeconds());
return `${year}-${month}-${day}` + `T${hours}:${minutes}:${seconds}Z`;
}
static cloneDate(date) {
return new Date(date.getTime());
}
static addLeadingZeroIfNeeded(value) {
return value > 9 ? String(value) : '0' + value;
}
}