Commit 29a7194 1 parent ba04864 commit 29a7194 Copy full SHA for 29a7194
File tree 2 files changed +36
-0
lines changed
src/packages/emmett/src/validation
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { ValidationError } from '../errors' ;
2
+
3
+ export const formatDateToUtcYYYYMMDD = ( date : Date ) => {
4
+ // Use the 'en-CA' locale which formats as 'yyyy-mm-dd'
5
+ const formatter = new Intl . DateTimeFormat ( 'en-CA' , {
6
+ timeZone : 'UTC' ,
7
+ year : 'numeric' ,
8
+ month : '2-digit' ,
9
+ day : '2-digit' ,
10
+ } ) ;
11
+
12
+ // Format the date
13
+ return formatter . format ( date ) ;
14
+ } ;
15
+
16
+ // Function to validate 'yyyy-mm-dd' format
17
+ export const isValidYYYYMMDD = ( dateString : string ) => {
18
+ const regex = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
19
+ return regex . test ( dateString ) ;
20
+ } ;
21
+
22
+ export const parseDateFromUtcYYYYMMDD = ( dateString : string ) => {
23
+ const date = new Date ( dateString + 'T00:00:00Z' ) ;
24
+
25
+ if ( ! isValidYYYYMMDD ( dateString ) ) {
26
+ throw new ValidationError ( 'Invalid date format, must be yyyy-mm-dd' ) ;
27
+ }
28
+
29
+ if ( isNaN ( date . getTime ( ) ) ) {
30
+ throw new ValidationError ( 'Invalid date format' ) ;
31
+ }
32
+
33
+ return date ;
34
+ } ;
Original file line number Diff line number Diff line change @@ -33,3 +33,5 @@ export const assertUnsignedBigInt = (value: string): bigint => {
33
33
}
34
34
return number ;
35
35
} ;
36
+
37
+ export * from './dates' ;
You can’t perform that action at this time.
0 commit comments