@@ -10,20 +10,37 @@ export const toLocalISOString = (date: Date): string => {
1010 return `${ year } -${ month } -${ day } T${ hours } :${ minutes } :${ seconds } .${ milliseconds } ` ;
1111} ;
1212
13- export const getFormatDatetime = ( datetime : Date | null , format : string ) => {
14- if ( ! datetime ) return '' ;
15- const origin = toLocalISOString ( datetime ) . split ( 'T' ) [ 0 ] as string ;
13+ export const formatDate = ( dateObj : Date | null , format : string ) => {
14+ if ( ! dateObj ) return '' ;
15+ const dateStr = toLocalISOString ( dateObj ) . split ( 'T' ) [ 0 ] as string ;
1616
17- const year = origin . split ( '-' ) [ 0 ] ;
18- const month = origin . split ( '-' ) [ 1 ] ;
19- const date = origin . split ( '-' ) [ 2 ] ;
17+ const year = dateStr . split ( '-' ) [ 0 ] ;
18+ const month = dateStr . split ( '-' ) [ 1 ] ;
19+ const date = dateStr . split ( '-' ) [ 2 ] ;
2020
21- const result = format
22- . replace ( / Y Y Y Y / g, String ( year ) )
23- . replace ( / M M / g, String ( month ) )
24- . replace ( / D D / g, String ( date ) ) ;
21+ if (
22+ / .* Y Y Y Y .* / . test ( format ) &&
23+ / .* M M .* / . test ( format ) &&
24+ / .* D D .* / . test ( format )
25+ ) {
26+ return format
27+ . replace ( / Y Y Y Y / g, String ( year ) )
28+ . replace ( / M M / g, String ( month ) )
29+ . replace ( / D D / g, String ( date ) ) ;
30+ }
31+
32+ return dateStr ;
33+ } ;
34+
35+ export const formatLabel = ( label : string , format : string ) => {
36+ const year = label . split ( '-' ) [ 0 ] ;
37+ const month = label . split ( '-' ) [ 1 ] ;
2538
26- return result ;
39+ if ( / .* Y Y Y Y .* / . test ( format ) && / .* M M .* / . test ( format ) ) {
40+ return format . replace ( / Y Y Y Y / g, String ( year ) ) . replace ( / M M / g, String ( month ) ) ;
41+ }
42+
43+ return label ;
2744} ;
2845
2946export const getMonthArray = ( year : number , month : number ) => {
@@ -32,7 +49,7 @@ export const getMonthArray = (year: number, month: number) => {
3249
3350 for ( let day = 1 ; day <= daysInMonth ; day ++ ) {
3451 const date = new Date ( year , month - 1 , day , 12 , 0 , 0 ) ;
35- const formattedDate = getFormatDatetime ( date , 'YYYY-MM-DD' ) ;
52+ const formattedDate = formatDate ( date , 'YYYY-MM-DD' ) ;
3653 const dayValue = date . getDate ( ) ;
3754 const dayOfWeek = date . getDay ( ) ;
3855
@@ -45,3 +62,17 @@ export const getMonthArray = (year: number, month: number) => {
4562
4663 return monthArray ;
4764} ;
65+
66+ // export const get;
67+
68+ /**
69+ * most pupolar date format
70+ * YYYY-MM-DD - 국제 표준
71+ * DD/MM/YYYY
72+ * DD-MM-YYYY
73+ * MM/DD/YYYY
74+ * MM-DD-YYYY
75+ * YYYY년 MM월 DD일 (한국 스타일)
76+ * DD Month YYYY (영국 스타일) - 04 September 2023
77+ * Month DD, YYYY (미국 스타일) - September 04, 2023
78+ */
0 commit comments