1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00

Datetime module improvement (#259)

This commit is contained in:
Vincent LE GOFF 2019-03-11 16:00:30 +01:00 committed by Ryan Dahl
parent 2153fd2646
commit 5852cd251b
2 changed files with 67 additions and 45 deletions

View file

@ -10,18 +10,23 @@ export type DateFormat = "mm-dd-yyyy" | "dd-mm-yyyy" | "yyyy-mm-dd";
*/ */
export function parseDate(dateStr: string, format: DateFormat): Date { export function parseDate(dateStr: string, format: DateFormat): Date {
let m, d, y: string; let m, d, y: string;
let datePattern: RegExp;
if (format === "mm-dd-yyyy") { switch (format) {
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/; case "mm-dd-yyyy":
[, m, d, y] = datePattern.exec(dateStr)!; datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
} else if (format === "dd-mm-yyyy") { [, m, d, y] = datePattern.exec(dateStr)!;
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/; break;
[, d, m, y] = datePattern.exec(dateStr)!; case "dd-mm-yyyy":
} else if (format === "yyyy-mm-dd") { datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
const datePattern = /^(\d{4})-(\d{2})-(\d{2})$/; [, d, m, y] = datePattern.exec(dateStr)!;
[, y, m, d] = datePattern.exec(dateStr)!; break;
} else { case "yyyy-mm-dd":
throw new Error("Invalid date format!"); datePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
[, y, m, d] = datePattern.exec(dateStr)!;
break;
default:
throw new Error("Invalid date format!");
} }
return new Date(Number(y), Number(m) - 1, Number(d)); return new Date(Number(y), Number(m) - 1, Number(d));
@ -47,42 +52,59 @@ export function parseDateTime(
format: DateTimeFormat format: DateTimeFormat
): Date { ): Date {
let m, d, y, ho, mi: string; let m, d, y, ho, mi: string;
let datePattern: RegExp;
if (format === "mm-dd-yyyy hh:mm") { switch (format) {
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/; case "mm-dd-yyyy hh:mm":
[, m, d, y, ho, mi] = datePattern.exec(datetimeStr)!; datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
} else if (format === "dd-mm-yyyy hh:mm") { [, m, d, y, ho, mi] = datePattern.exec(datetimeStr)!;
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/; break;
[, d, m, y, ho, mi] = datePattern.exec(datetimeStr)!; case "dd-mm-yyyy hh:mm":
} else if (format === "yyyy-mm-dd hh:mm") { datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
const datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/; [, d, m, y, ho, mi] = datePattern.exec(datetimeStr)!;
[, y, m, d, ho, mi] = datePattern.exec(datetimeStr)!; break;
} else if (format === "hh:mm mm-dd-yyyy") { case "yyyy-mm-dd hh:mm":
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/; datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
[, ho, mi, m, d, y] = datePattern.exec(datetimeStr)!; [, y, m, d, ho, mi] = datePattern.exec(datetimeStr)!;
} else if (format === "hh:mm dd-mm-yyyy") { break;
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/; case "hh:mm mm-dd-yyyy":
[, ho, mi, d, m, y] = datePattern.exec(datetimeStr)!; datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
} else if (format === "hh:mm yyyy-mm-dd") { [, ho, mi, m, d, y] = datePattern.exec(datetimeStr)!;
const datePattern = /^(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2})$/; break;
[, ho, mi, y, m, d] = datePattern.exec(datetimeStr)!; case "hh:mm dd-mm-yyyy":
} else { datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
throw new Error("Invalid datetime format!"); [, 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!");
} }
return new Date(Number(y), Number(m) - 1, Number(d), Number(ho), Number(mi)); return new Date(Number(y), Number(m) - 1, Number(d), Number(ho), Number(mi));
} }
/**
* Get number of the day in the year
* @return {number} Number of the day in year
*/
export function dayOfYear(date: Date): any {
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);
}
/** /**
* Get number of current day in year * Get number of current day in year
* *
* @return {number} Number of current day in year * @return {number} Number of current day in year
*/ */
export function currentDayOfYear(): number { export function currentDayOfYear(): number {
return ( return dayOfYear(new Date());
Math.ceil(new Date().getTime() / 86400000) -
Math.floor(
new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000
)
);
} }

View file

@ -65,12 +65,12 @@ test(function invalidParseDateFormatThrows() {
} }
}); });
test(function currentDayOfYear() { test(function DayOfYear() {
assertEquals( assertEquals(1, datetime.dayOfYear(new Date("2019-01-01T03:24:00")));
datetime.currentDayOfYear(), assertEquals(70, datetime.dayOfYear(new Date("2019-03-11T03:24:00")));
Math.ceil(new Date().getTime() / 86400000) - assertEquals(365, datetime.dayOfYear(new Date("2019-12-31T03:24:00")));
Math.floor( });
new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000
) test(function currentDayOfYear() {
); assertEquals(datetime.currentDayOfYear(), datetime.dayOfYear(new Date()));
}); });