Skip to content

Commit 1c14ff9

Browse files
author
Marius Milea
committed
Add todayStringNoHyphens()
todayStringNoHyphens() will return today's date in the following format: '20240617'
1 parent 61dcad5 commit 1c14ff9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/datetime/localDate.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@ test('todayString', () => {
352352
expect(s < '2099-01-01').toBe(true)
353353
})
354354

355+
test('todayStringNoHyphens', () => {
356+
const s = todayString()
357+
expect(s.startsWith(new Date().getFullYear())).toBe(true)
358+
expect(s > '2024-05-01').toBe(true)
359+
expect(s < '2099-01-01').toBe(true)
360+
})
361+
355362
test('todayString tz', () => {
356363
if (isUTC()) return
357364
console.log(process.env['TZ'])

src/datetime/localDate.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,13 @@ Object.setPrototypeOf(localDate, localDateFactory)
751751
export function todayString(): IsoDateString {
752752
return localDate.fromDate(new Date()).toISODate()
753753
}
754+
755+
/**
756+
Convenience function to return current today's IsoDateString representation formatted with no hyphens, e.g `20240613`
757+
*/
758+
export function todayStringNoHyphens(): IsoDateString {
759+
const date = new Date();
760+
const ymd = date.toISOString().split('T')[0].split('-');
761+
const date_formatted = `${ymd[0]}${ymd[1]}${ymd[2]}`
762+
return date_formatted
763+
}

0 commit comments

Comments
 (0)