2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-11 00:19:42 -05:00
|
|
|
export type DateFormat = "mm-dd-yyyy" | "dd-mm-yyyy" | "yyyy-mm-dd";
|
|
|
|
|
2019-01-03 10:19:20 -05:00
|
|
|
/**
|
|
|
|
* Parse date from string using format string
|
|
|
|
*
|
|
|
|
* @param {string} dateStr - date string
|
2019-01-11 00:19:42 -05:00
|
|
|
* @param {DateFormat} format - format string
|
2019-01-03 10:19:20 -05:00
|
|
|
* @return {Date} Parsed date
|
|
|
|
*/
|
2019-01-11 00:19:42 -05:00
|
|
|
export function parseDate(dateStr: string, format: DateFormat): Date {
|
2019-01-03 10:19:20 -05:00
|
|
|
let m, d, y: string;
|
|
|
|
|
|
|
|
if (format === "mm-dd-yyyy") {
|
|
|
|
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, m, d, y] = datePattern.exec(dateStr);
|
|
|
|
} else if (format === "dd-mm-yyyy") {
|
|
|
|
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, d, m, y] = datePattern.exec(dateStr);
|
|
|
|
} else if (format === "yyyy-mm-dd") {
|
|
|
|
const datePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
|
|
[, y, m, d] = datePattern.exec(dateStr);
|
2019-01-11 00:19:42 -05:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid date format!");
|
2019-01-03 10:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(Number(y), Number(m) - 1, Number(d));
|
|
|
|
}
|
|
|
|
|
2019-01-11 00:19:42 -05:00
|
|
|
export type DateTimeFormat =
|
|
|
|
| "mm-dd-yyyy hh:mm"
|
|
|
|
| "dd-mm-yyyy hh:mm"
|
|
|
|
| "yyyy-mm-dd hh:mm"
|
|
|
|
| "hh:mm mm-dd-yyyy"
|
|
|
|
| "hh:mm dd-mm-yyyy"
|
|
|
|
| "hh:mm yyyy-mm-dd";
|
|
|
|
|
2019-01-03 10:19:20 -05:00
|
|
|
/**
|
|
|
|
* Parse date & time from string using format string
|
|
|
|
*
|
|
|
|
* @param {string} dateStr - date & time string
|
2019-01-11 00:19:42 -05:00
|
|
|
* @param {DateTimeFormat} format - format string
|
2019-01-03 10:19:20 -05:00
|
|
|
* @return {Date} Parsed date
|
|
|
|
*/
|
2019-01-11 00:19:42 -05:00
|
|
|
export function parseDateTime(
|
|
|
|
datetimeStr: string,
|
|
|
|
format: DateTimeFormat
|
|
|
|
): Date {
|
2019-01-03 10:19:20 -05:00
|
|
|
let m, d, y, ho, mi: string;
|
|
|
|
|
|
|
|
if (format === "mm-dd-yyyy hh:mm") {
|
|
|
|
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
|
|
|
|
[, m, d, y, ho, mi] = datePattern.exec(datetimeStr);
|
|
|
|
} else if (format === "dd-mm-yyyy hh:mm") {
|
|
|
|
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
|
|
|
|
[, d, m, y, ho, mi] = datePattern.exec(datetimeStr);
|
|
|
|
} else if (format === "yyyy-mm-dd hh:mm") {
|
|
|
|
const datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
|
|
|
|
[, y, m, d, ho, mi] = datePattern.exec(datetimeStr);
|
|
|
|
} else if (format === "hh:mm mm-dd-yyyy") {
|
2019-01-16 10:25:52 -05:00
|
|
|
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
|
2019-01-03 10:19:20 -05:00
|
|
|
[, ho, mi, m, d, y] = datePattern.exec(datetimeStr);
|
|
|
|
} else if (format === "hh:mm dd-mm-yyyy") {
|
2019-01-16 10:25:52 -05:00
|
|
|
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
|
2019-01-03 10:19:20 -05:00
|
|
|
[, ho, mi, d, m, y] = datePattern.exec(datetimeStr);
|
|
|
|
} else if (format === "hh:mm yyyy-mm-dd") {
|
2019-01-16 10:25:52 -05:00
|
|
|
const datePattern = /^(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2})$/;
|
2019-01-03 10:19:20 -05:00
|
|
|
[, ho, mi, y, m, d] = datePattern.exec(datetimeStr);
|
2019-01-11 00:19:42 -05:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid datetime format!");
|
2019-01-03 10:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(Number(y), Number(m) - 1, Number(d), Number(ho), Number(mi));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get number of current day in year
|
|
|
|
*
|
|
|
|
* @return {number} Number of current day in year
|
|
|
|
*/
|
|
|
|
export function currentDayOfYear(): number {
|
|
|
|
return (
|
|
|
|
Math.ceil(new Date().getTime() / 86400000) -
|
|
|
|
Math.floor(
|
|
|
|
new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|