From b4906387469eba24f868082b263dd44d89a40db7 Mon Sep 17 00:00:00 2001 From: "Dmitry Sharshakov aka. sh7dm" Date: Thu, 3 Jan 2019 18:19:20 +0300 Subject: [PATCH] Add datetime utils (#72) --- datetime/mod.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ datetime/test.ts | 24 +++++++++++++++++ test.ts | 1 + 3 files changed, 95 insertions(+) create mode 100644 datetime/mod.ts create mode 100644 datetime/test.ts diff --git a/datetime/mod.ts b/datetime/mod.ts new file mode 100644 index 0000000000..db66af1772 --- /dev/null +++ b/datetime/mod.ts @@ -0,0 +1,70 @@ +/** + * Parse date from string using format string + * + * @param {string} dateStr - date string + * @param {string} format - format string + * @return {Date} Parsed date + */ +export function parseDate(dateStr: string, format: string): Date { + 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); + } + + return new Date(Number(y), Number(m) - 1, Number(d)); +} + +/** + * Parse date & time from string using format string + * + * @param {string} dateStr - date & time string + * @param {string} format - format string + * @return {Date} Parsed date + */ +export function parseDateTime(datetimeStr: string, format: string): Date { + 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") { + const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/; + [, ho, mi, m, d, y] = datePattern.exec(datetimeStr); + } else if (format === "hh:mm dd-mm-yyyy") { + const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/; + [, ho, mi, d, m, y] = datePattern.exec(datetimeStr); + } else if (format === "hh:mm yyyy-mm-dd") { + const datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/; + [, ho, mi, y, m, d] = datePattern.exec(datetimeStr); + } + + 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 + ) + ); +} diff --git a/datetime/test.ts b/datetime/test.ts new file mode 100644 index 0000000000..d5c1622cc3 --- /dev/null +++ b/datetime/test.ts @@ -0,0 +1,24 @@ +import { test, assertEqual } from "../testing/mod.ts"; +import * as datetime from "mod.ts"; + +test(function parseDateTime() { + assertEqual( + datetime.parseDateTime("01-03-2019 16:34", "mm-dd-yyyy hh:mm"), + new Date(2019, 1, 3, 16, 34) + ); +}); +test(function parseDate() { + assertEqual( + datetime.parseDateTime("01-03-2019", "mm-dd-yyyy"), + new Date(2019, 1, 3) + ); +}); +test(function currentDayOfYear() { + assertEqual( + datetime.currentDayOfYear(), + Math.ceil(new Date().getTime() / 86400000) - + Math.floor( + new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000 + ) + ); +}); diff --git a/test.ts b/test.ts index 4c7f763af8..821aa41160 100755 --- a/test.ts +++ b/test.ts @@ -2,6 +2,7 @@ import { run } from "deno"; import "colors/main_test.ts"; +import "datetime/test.ts"; import "examples/test.ts"; import "flags/test.ts"; import "logging/test.ts";