2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-04-27 19:07:11 -04:00
|
|
|
import { pad } from "../strings/pad.ts";
|
|
|
|
|
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
|
2019-03-14 10:24:54 -04:00
|
|
|
* @param dateStr Date string
|
|
|
|
* @param format Format string
|
|
|
|
* @return Parsed date
|
2019-01-03 10:19:20 -05:00
|
|
|
*/
|
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;
|
2019-03-11 11:00:30 -04:00
|
|
|
let datePattern: RegExp;
|
2019-01-03 10:19:20 -05:00
|
|
|
|
2019-03-11 11:00:30 -04:00
|
|
|
switch (format) {
|
|
|
|
case "mm-dd-yyyy":
|
|
|
|
datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, m, d, y] = datePattern.exec(dateStr)!;
|
|
|
|
break;
|
|
|
|
case "dd-mm-yyyy":
|
|
|
|
datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, d, m, y] = datePattern.exec(dateStr)!;
|
|
|
|
break;
|
|
|
|
case "yyyy-mm-dd":
|
|
|
|
datePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
|
|
[, y, m, d] = datePattern.exec(dateStr)!;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
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
|
2019-03-14 10:24:54 -04:00
|
|
|
* @param dateStr Date & time string
|
|
|
|
* @param format Format string
|
|
|
|
* @return Parsed date
|
2019-01-03 10:19:20 -05:00
|
|
|
*/
|
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;
|
2019-03-11 11:00:30 -04:00
|
|
|
let datePattern: RegExp;
|
2019-01-03 10:19:20 -05:00
|
|
|
|
2019-03-11 11:00:30 -04:00
|
|
|
switch (format) {
|
|
|
|
case "mm-dd-yyyy hh:mm":
|
|
|
|
datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
|
|
|
|
[, m, d, y, ho, mi] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
case "dd-mm-yyyy hh:mm":
|
|
|
|
datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
|
|
|
|
[, d, m, y, ho, mi] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
case "yyyy-mm-dd hh:mm":
|
|
|
|
datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
|
|
|
|
[, y, m, d, ho, mi] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
case "hh:mm mm-dd-yyyy":
|
|
|
|
datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, ho, mi, m, d, y] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
case "hh:mm dd-mm-yyyy":
|
|
|
|
datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
|
|
|
|
[, ho, mi, d, m, y] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
case "hh:mm yyyy-mm-dd":
|
|
|
|
datePattern = /^(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2})$/;
|
|
|
|
[, ho, mi, y, m, d] = datePattern.exec(datetimeStr)!;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-03-11 11:00:30 -04:00
|
|
|
/**
|
|
|
|
* Get number of the day in the year
|
2019-03-14 10:24:54 -04:00
|
|
|
* @return Number of the day in year
|
2019-03-11 11:00:30 -04:00
|
|
|
*/
|
2019-03-14 10:24:54 -04:00
|
|
|
export function dayOfYear(date: Date): number {
|
2019-03-11 11:00:30 -04:00
|
|
|
const dayMs = 1000 * 60 * 60 * 24;
|
|
|
|
const yearStart = new Date(date.getFullYear(), 0, 0);
|
|
|
|
const diff =
|
|
|
|
date.getTime() -
|
|
|
|
yearStart.getTime() +
|
|
|
|
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
|
|
|
|
return Math.floor(diff / dayMs);
|
|
|
|
}
|
|
|
|
|
2019-01-03 10:19:20 -05:00
|
|
|
/**
|
|
|
|
* Get number of current day in year
|
2019-03-14 10:24:54 -04:00
|
|
|
* @return Number of current day in year
|
2019-01-03 10:19:20 -05:00
|
|
|
*/
|
|
|
|
export function currentDayOfYear(): number {
|
2019-03-11 11:00:30 -04:00
|
|
|
return dayOfYear(new Date());
|
2019-01-03 10:19:20 -05:00
|
|
|
}
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a date to return a IMF formated string date
|
|
|
|
* RFC: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
|
|
|
|
* IMF is the time format to use when generating times in HTTP
|
|
|
|
* headers. The time being formatted must be in UTC for Format to
|
|
|
|
* generate the correct format.
|
|
|
|
* @param date Date to parse
|
|
|
|
* @return IMF date formated string
|
|
|
|
*/
|
|
|
|
export function toIMF(date: Date): string {
|
2019-10-05 12:02:34 -04:00
|
|
|
function dtPad(v: string, lPad = 2): string {
|
2019-04-27 19:07:11 -04:00
|
|
|
return pad(v, lPad, { char: "0" });
|
|
|
|
}
|
|
|
|
const d = dtPad(date.getUTCDate().toString());
|
|
|
|
const h = dtPad(date.getUTCHours().toString());
|
|
|
|
const min = dtPad(date.getUTCMinutes().toString());
|
|
|
|
const s = dtPad(date.getUTCSeconds().toString());
|
|
|
|
const y = date.getUTCFullYear();
|
2019-11-16 08:24:07 -05:00
|
|
|
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
2019-04-27 19:07:11 -04:00
|
|
|
const months = [
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
2019-11-16 08:24:07 -05:00
|
|
|
"Apr",
|
2019-04-27 19:07:11 -04:00
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec"
|
|
|
|
];
|
2019-05-13 14:03:24 -04:00
|
|
|
return `${days[date.getUTCDay()]}, ${d} ${
|
2019-04-27 19:07:11 -04:00
|
|
|
months[date.getUTCMonth()]
|
|
|
|
} ${y} ${h}:${min}:${s} GMT`;
|
|
|
|
}
|