From 1c14ff9c6124158d6a6bb038ab1b2a41407fbbed Mon Sep 17 00:00:00 2001 From: Marius Milea Date: Mon, 17 Jun 2024 20:33:22 +0300 Subject: [PATCH] Add todayStringNoHyphens() todayStringNoHyphens() will return today's date in the following format: '20240617' --- src/datetime/localDate.test.ts | 7 +++++++ src/datetime/localDate.ts | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/datetime/localDate.test.ts b/src/datetime/localDate.test.ts index 5acd1dec..daef1f23 100644 --- a/src/datetime/localDate.test.ts +++ b/src/datetime/localDate.test.ts @@ -352,6 +352,13 @@ test('todayString', () => { expect(s < '2099-01-01').toBe(true) }) +test('todayStringNoHyphens', () => { + const s = todayString() + expect(s.startsWith(new Date().getFullYear())).toBe(true) + expect(s > '2024-05-01').toBe(true) + expect(s < '2099-01-01').toBe(true) +}) + test('todayString tz', () => { if (isUTC()) return console.log(process.env['TZ']) diff --git a/src/datetime/localDate.ts b/src/datetime/localDate.ts index 388a4bbc..ec891fcd 100644 --- a/src/datetime/localDate.ts +++ b/src/datetime/localDate.ts @@ -751,3 +751,13 @@ Object.setPrototypeOf(localDate, localDateFactory) export function todayString(): IsoDateString { return localDate.fromDate(new Date()).toISODate() } + +/** + Convenience function to return current today's IsoDateString representation formatted with no hyphens, e.g `20240613` + */ +export function todayStringNoHyphens(): IsoDateString { + const date = new Date(); + const ymd = date.toISOString().split('T')[0].split('-'); + const date_formatted = `${ymd[0]}${ymd[1]}${ymd[2]}` + return date_formatted +}