Skip to content

Commit

Permalink
feat: add date util
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Dec 18, 2023
1 parent 11dc3be commit 83cab55
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [labeled]

env:
NODE_VERSION: 18
NODE_VERSION: 20

jobs:
security:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
- main

env:
NODE_VERSION: 18
NODE_VERSION: 20

jobs:
test:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/date/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions packages/date/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +14,7 @@ module.exports = {
getDayMonthYear,
getFullRelativeTime,
getHourMinutes,
getIsoRelativeTime,
getISOAndRelativeTime,
getRelativeTime,
getYearMonthDay,
revYearMonthDay,
Expand Down
5 changes: 5 additions & 0 deletions utils/date/getCurrentISO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getISO = require('./toISO')
const getNow = require('./getNow')

// get MS value of current time
module.exports = () => getISO(getNow())
5 changes: 5 additions & 0 deletions utils/date/getCurrentMs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getMs = require('./getMs')
const getNow = require('./getNow')

// get MS value of current time
module.exports = () => getMs(getNow())
File renamed without changes.
2 changes: 2 additions & 0 deletions utils/date/getMs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// get MS value of timestamp
module.exports = (date) => date.valueOf()
4 changes: 4 additions & 0 deletions utils/date/getNow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { DateTime } = require('luxon')

// get iso date with relative years
module.exports = () => DateTime.now()
3 changes: 3 additions & 0 deletions utils/date/getRelativeTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ const { DateTime } = require('luxon')

// get relative years (returns 'in YY Jahren')
module.exports = (date) => DateTime.fromISO(date).setLocale('de').toRelative()

Check warning on line 5 in utils/date/getRelativeTime.js

View workflow job for this annotation

GitHub Actions / Run Mocha Tests

Delete `⏎⏎⏎`


4 changes: 0 additions & 4 deletions utils/date/getYearMonthDay.js

This file was deleted.

4 changes: 4 additions & 0 deletions utils/date/parseISO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { DateTime } = require('luxon')

// parse ISO format
module.exports = (iso) => DateTime.fromISO(iso)
2 changes: 2 additions & 0 deletions utils/date/toISO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// get ISO format
module.exports = (date) => date.toUTC().toISO()
7 changes: 7 additions & 0 deletions utils/date/toYearMonthDay.js
Original file line number Diff line number Diff line change
@@ -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'))

0 comments on commit 83cab55

Please sign in to comment.