1/** 2 * 3 * Copyright (c) 2025 Huawei Device Co., Ltd. 4 * 5 * All rights reserved. 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 9 * Redistributions of source code must retain the above copyright notice,this 10 * list of conditions and the following disclaimer. 11 * 12 * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, 17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, 23 * 24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26import { DateModel } from './DateModel'; 27 28const SATURDAY = 6; // 日历表上周六对应的序列号,从周日开始算起,取值0~6 29 30/* 31 * 根据指定年份和月份获取该月在日历表上的日期排布数据 32 * @param { number } specifiedMonth - 指定月份 33 * @param { number } specifiedYear - 指定年份 34 */ 35export function getMonthDate(specifiedMonth: number, specifiedYear: number): number[] { 36 let currentFirstWeekDay: number = 0; // 初始化指定月的第一天是周几 37 let currentLastWeekDay: number = 0; // 初始化指定月的最后一天是周几 38 let currentAllDay: number[] = []; // 初始化指定月的日期排列数组 39 let totalDays = new Date(specifiedYear, specifiedMonth, 0).getDate(); // 初始化指定月总天数 40 currentFirstWeekDay = new Date(specifiedYear, specifiedMonth - 1, 1).getDay(); // 获取指定月的第一天是周几 41 currentLastWeekDay = new Date(specifiedYear, specifiedMonth - 1, totalDays).getDay(); // 获取指定月的最后一天是周几 42 // 将月份中显示上个月日期的内容置0 43 for (let item = 0; item < currentFirstWeekDay; item++) { 44 currentAllDay.push(0); 45 } 46 // 将本月日期内容存入数组 47 for (let item = 1; item <= totalDays; item++) { 48 currentAllDay.push(item); 49 } 50 // 将月份中显示下个月日期的内容置0 51 for (let item = 0; item < SATURDAY - currentLastWeekDay; item++) { 52 currentAllDay.push(0); 53 } 54 return currentAllDay; 55} 56 57/* 58 * 获取当前日期,年月日星期几 59 */ 60export function getRealTimeDate(): DateModel { 61 const nowDate = new Date(); // 创建Date对象,设置当前日期和时间 62 let currentMonth = nowDate.getMonth() + 1; // 获取当前月份,getMonth()获得的值是0~11,实际月份需要+1 63 let currentDay = nowDate.getDate(); // 获取当前日 64 let currentYear = nowDate.getFullYear(); // 获取当前年份 65 let currentWeekDay = new Date(currentYear, currentMonth - 1, currentDay).getDay(); // 获取当前星期几 66 let nowDateModel = new DateModel(0, 0, 0, 0); // 创建DateModel实例 67 nowDateModel.day = currentDay; 68 nowDateModel.week = currentWeekDay; 69 nowDateModel.month = currentMonth; 70 nowDateModel.year = currentYear; 71 return nowDateModel; 72}