From 83cab550ac98f61a9973b6367c92f9a98ba7e8f6 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Mon, 18 Dec 2023 14:25:09 +0100 Subject: [PATCH] feat: add date util --- .github/workflows/pull.yml | 2 +- .github/workflows/push.yml | 2 +- CHANGELOG.md | 5 +++++ packages/date/README.md | 5 +++++ packages/date/index.js | 6 +++--- utils/date/getCurrentISO.js | 5 +++++ utils/date/getCurrentMs.js | 5 +++++ .../{getIsoRelativeTime.js => getISOAndRelativeTime.js} | 0 utils/date/getMs.js | 2 ++ utils/date/getNow.js | 4 ++++ utils/date/getRelativeTime.js | 3 +++ utils/date/getYearMonthDay.js | 4 ---- utils/date/parseISO.js | 4 ++++ utils/date/toISO.js | 2 ++ utils/date/toYearMonthDay.js | 7 +++++++ 15 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 utils/date/getCurrentISO.js create mode 100644 utils/date/getCurrentMs.js rename utils/date/{getIsoRelativeTime.js => getISOAndRelativeTime.js} (100%) create mode 100644 utils/date/getMs.js create mode 100644 utils/date/getNow.js delete mode 100644 utils/date/getYearMonthDay.js create mode 100644 utils/date/parseISO.js create mode 100644 utils/date/toISO.js create mode 100644 utils/date/toYearMonthDay.js diff --git a/.github/workflows/pull.yml b/.github/workflows/pull.yml index cdd067a..6579966 100644 --- a/.github/workflows/pull.yml +++ b/.github/workflows/pull.yml @@ -5,7 +5,7 @@ on: types: [labeled] env: - NODE_VERSION: 18 + NODE_VERSION: 20 jobs: security: diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 383fde0..59dfbc1 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -10,7 +10,7 @@ on: - main env: - NODE_VERSION: 18 + NODE_VERSION: 20 jobs: test: diff --git a/CHANGELOG.md b/CHANGELOG.md index 43fa234..ed79520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog +- 2023-06-16 - v1.2.0 + - feat: `getYearMonthDay` can now return a date with strings (YYYY-MM-DD) + - refactor!: `getYearMonthDay` now returns an integer + - refactor!: `getIsoRelativeTime` is now called `getISOAndRelativeTime` + - 2023-04-19 - v1.1.3 - fix: don't run linter on dependabot-pr - fix: use `toReadable` in `pluralize` helper diff --git a/packages/date/README.md b/packages/date/README.md index f85a0b6..da97b10 100644 --- a/packages/date/README.md +++ b/packages/date/README.md @@ -135,6 +135,11 @@ const { getYearMonthDay } = require('@swrlab/utils/packages/date') Then use the toolkit: +```js +getYearMonthDay('2038-01-19T03:14:08.000', true) +// '2038-01-19' +``` + ```js getYearMonthDay('2038-01-19T03:14:08.000') // '20380119' diff --git a/packages/date/index.js b/packages/date/index.js index 4ab424e..8df4a7a 100644 --- a/packages/date/index.js +++ b/packages/date/index.js @@ -3,9 +3,9 @@ const getDateHourMinutes = require('../../utils/date/getDateHourMinutes') const getDayMonthYear = require('../../utils/date/getDayMonthYear') const getFullRelativeTime = require('../../utils/date/getFullRelativeTime') const getHourMinutes = require('../../utils/date/getHourMinutes') -const getIsoRelativeTime = require('../../utils/date/getIsoRelativeTime') +const getISOAndRelativeTime = require('../../utils/date/getISOAndRelativeTime') const getRelativeTime = require('../../utils/date/getRelativeTime') -const getYearMonthDay = require('../../utils/date/getYearMonthDay') +const getYearMonthDay = require('../../utils/date/toYearMonthDay') const revYearMonthDay = require('../../utils/date/revYearMonthDay') // export packages @@ -14,7 +14,7 @@ module.exports = { getDayMonthYear, getFullRelativeTime, getHourMinutes, - getIsoRelativeTime, + getISOAndRelativeTime, getRelativeTime, getYearMonthDay, revYearMonthDay, diff --git a/utils/date/getCurrentISO.js b/utils/date/getCurrentISO.js new file mode 100644 index 0000000..a53ee62 --- /dev/null +++ b/utils/date/getCurrentISO.js @@ -0,0 +1,5 @@ +const getISO = require('./toISO') +const getNow = require('./getNow') + +// get MS value of current time +module.exports = () => getISO(getNow()) diff --git a/utils/date/getCurrentMs.js b/utils/date/getCurrentMs.js new file mode 100644 index 0000000..41b1fb8 --- /dev/null +++ b/utils/date/getCurrentMs.js @@ -0,0 +1,5 @@ +const getMs = require('./getMs') +const getNow = require('./getNow') + +// get MS value of current time +module.exports = () => getMs(getNow()) diff --git a/utils/date/getIsoRelativeTime.js b/utils/date/getISOAndRelativeTime.js similarity index 100% rename from utils/date/getIsoRelativeTime.js rename to utils/date/getISOAndRelativeTime.js diff --git a/utils/date/getMs.js b/utils/date/getMs.js new file mode 100644 index 0000000..91d6c36 --- /dev/null +++ b/utils/date/getMs.js @@ -0,0 +1,2 @@ +// get MS value of timestamp +module.exports = (date) => date.valueOf() diff --git a/utils/date/getNow.js b/utils/date/getNow.js new file mode 100644 index 0000000..1e9aa2e --- /dev/null +++ b/utils/date/getNow.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// get iso date with relative years +module.exports = () => DateTime.now() diff --git a/utils/date/getRelativeTime.js b/utils/date/getRelativeTime.js index 0df4aa8..e07720a 100644 --- a/utils/date/getRelativeTime.js +++ b/utils/date/getRelativeTime.js @@ -2,3 +2,6 @@ const { DateTime } = require('luxon') // get relative years (returns 'in YY Jahren') module.exports = (date) => DateTime.fromISO(date).setLocale('de').toRelative() + + + diff --git a/utils/date/getYearMonthDay.js b/utils/date/getYearMonthDay.js deleted file mode 100644 index 1b778a8..0000000 --- a/utils/date/getYearMonthDay.js +++ /dev/null @@ -1,4 +0,0 @@ -const { DateTime } = require('luxon') - -// get YYYYMMDD (returns '19700101') -module.exports = (date) => DateTime.fromISO(date).setLocale('de').toFormat('yyyyLLdd') diff --git a/utils/date/parseISO.js b/utils/date/parseISO.js new file mode 100644 index 0000000..278a465 --- /dev/null +++ b/utils/date/parseISO.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// parse ISO format +module.exports = (iso) => DateTime.fromISO(iso) diff --git a/utils/date/toISO.js b/utils/date/toISO.js new file mode 100644 index 0000000..99acc63 --- /dev/null +++ b/utils/date/toISO.js @@ -0,0 +1,2 @@ +// get ISO format +module.exports = (date) => date.toUTC().toISO() diff --git a/utils/date/toYearMonthDay.js b/utils/date/toYearMonthDay.js new file mode 100644 index 0000000..178c361 --- /dev/null +++ b/utils/date/toYearMonthDay.js @@ -0,0 +1,7 @@ +const { DateTime } = require('luxon') + +// get YYYYMMDD (returns '19700101') +module.exports = (date, withDashes) => + withDashes + ? DateTime.fromISO(date).setLocale('de').toFormat('yyyy-LL-dd') + : parseInt(DateTime.fromISO(date).setLocale('de').toFormat('yyyyLLdd'))