Browse Source

feat: 详情增加日期

Yaavi 1 year ago
parent
commit
67975bcd40
4 changed files with 16 additions and 8 deletions
  1. 2 2
      README.md
  2. 4 0
      demo/index.ts
  3. 1 1
      index.html
  4. 9 5
      src/holidays/index.ts

+ 2 - 2
README.md

@@ -67,11 +67,11 @@ const end = '2024-05-06';
 
 
 // 获取从 2024-05-01 到 2024-05-10 的所有节假日,包括周末
 // 获取从 2024-05-01 到 2024-05-10 的所有节假日,包括周末
 const holidaysIncludingWeekends = getHolidays(start, end, true);
 const holidaysIncludingWeekends = getHolidays(start, end, true);
-console.log('Holidays including weekends:', holidaysIncludingWeekends.map(d => d.format('YYYY-MM-DD')));
+console.log('Holidays including weekends:', holidaysIncludingWeekends.map(d => getDayDetail(d)));
 
 
 // 获取从 2024-05-01 到 2024-05-10 的节假日,不包括周末
 // 获取从 2024-05-01 到 2024-05-10 的节假日,不包括周末
 const holidaysExcludingWeekends = getHolidays(start, end, false);
 const holidaysExcludingWeekends = getHolidays(start, end, false);
-console.log('Holidays excluding weekends:', holidaysExcludingWeekends.map(d => d.format('YYYY-MM-DD')));
+console.log('Holidays excluding weekends:', holidaysExcludingWeekends.map(d => getDayDetail(d)));
 ```
 ```
 
 
 
 

+ 4 - 0
demo/index.ts

@@ -0,0 +1,4 @@
+import chinaDays from "../src";
+
+const holidays = chinaDays.getHolidays("2024-01-01", "2024-12-31", false)
+console.log('Holidays including weekends:', holidays.map(d => chinaDays.getDayDetail(d)));

+ 1 - 1
index.html

@@ -7,6 +7,6 @@
   </head>
   </head>
   <body>
   <body>
     <!-- <script type="module" src="./solar_terms/index.ts"></script> -->
     <!-- <script type="module" src="./solar_terms/index.ts"></script> -->
-    <script type="module" src="./src/index.ts"></script>
+    <script type="module" src="./demo/index.ts"></script>
   </body>
   </body>
 </html>
 </html>

+ 9 - 5
src/holidays/index.ts

@@ -41,21 +41,25 @@ const isInLieu = (date: dayjs.ConfigType): boolean => {
 }
 }
 
 
 /** 获取工作日详情 */
 /** 获取工作日详情 */
-const getDayDetail = (date: dayjs.ConfigType): { work: boolean, name: string } => {
+const getDayDetail = (date: dayjs.ConfigType): { work: boolean, name: string, date: string } => {
   date = _validateDate(date) as Dayjs;
   date = _validateDate(date) as Dayjs;
-  if (workdays[date.format('YYYY-MM-DD')]) {
+  const formattedDate = date.format('YYYY-MM-DD')
+  if (workdays[formattedDate]) {
     return {
     return {
+      date: formattedDate,
       work: true,
       work: true,
-      name: workdays[date.format('YYYY-MM-DD')]
+      name: workdays[formattedDate]
     }
     }
-  } else if (holidays[date.format('YYYY-MM-DD')]) {
+  } else if (holidays[formattedDate]) {
     return {
     return {
+      date: formattedDate,
       work: false,
       work: false,
-      name: holidays[date.format('YYYY-MM-DD')]
+      name: holidays[formattedDate]
     }
     }
   } else {
   } else {
     const weekday = date.day();
     const weekday = date.day();
     return {
     return {
+      date: formattedDate,
       work: weekday !== 0 && weekday !== 6,
       work: weekday !== 0 && weekday !== 6,
       name: date.format('dddd')
       name: date.format('dddd')
     }
     }