1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import {ConvertLunarCalendar} from '../../../../../common/src/main/ets/default/LunarCalendar' 16 17export class DateTimeCommon { 18 19 static getInstance(): DateTimeCommon { 20 if (globalThis.DateTimeCommon == null) { 21 globalThis.DateTimeCommon = new DateTimeCommon(); 22 } 23 return globalThis.DateTimeCommon; 24 } 25 26 getSystemTime(isUsing24hFormat: boolean) { 27 let dateTime = new Date(); 28 let hours = dateTime.getHours(); 29 if (!isUsing24hFormat && hours > 12) { 30 hours = hours % 12; 31 } 32 let minutes = dateTime.getMinutes(); 33 let time = this.concatTime(hours, minutes) 34 return time; 35 } 36 37 getSystemDate(): {} { 38 let dateTime = new Date(); 39 let result = { 40 'year': dateTime.getFullYear(), 41 'month': dateTime.getMonth() + 1, 42 'day': dateTime.getDate() 43 } 44 return result 45 } 46 47 getCalendarDate(): {} { 48 let dateTime = new Date(); 49 let res = { 50 'calendarYear': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarYear, 51 'calendarMonth': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarMonth, 52 'calendarDay': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarDay 53 } 54 return res 55 } 56 57 getSystemWeek() { 58 let dateTime = new Date(); 59 let days = dateTime.getDay(); 60 let week = this.convert(days) 61 return week 62 } 63 64 concatTime(hours, minutes): string{ 65 return `${this.fill(hours)}:${this.fill(minutes)}`; 66 }; 67 68 fill(value) { 69 return (value > 9 ? "" : "0") + value; 70 }; 71 72 convert(days) { 73 switch (days) { 74 case 0: 75 return $r('app.string.sunday'); 76 break; 77 case 1: 78 return $r('app.string.monday'); 79 break; 80 case 2: 81 return $r('app.string.tuesday'); 82 break; 83 case 3: 84 return $r('app.string.wednesday'); 85 break; 86 case 4: 87 return $r('app.string.thursday'); 88 break; 89 case 5: 90 return $r('app.string.friday'); 91 break; 92 case 6: 93 return $r('app.string.saturday'); 94 break; 95 default: 96 break; 97 } 98 } 99} 100 101export let dateTimeCommon = DateTimeCommon.getInstance();