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 getSystemTime(isUsing24hFormat: boolean) { 19 let dateTime = new Date(); 20 let hours = dateTime.getHours(); 21 if (!isUsing24hFormat && hours > 12) { 22 hours = hours % 12; 23 } 24 let minutes = dateTime.getMinutes(); 25 let time = this.concatTime(hours, minutes) 26 return time; 27 } 28 29 getSystemDate(): {} { 30 let dateTime = new Date(); 31 let result = { 32 'year': dateTime.getFullYear(), 33 'month': dateTime.getMonth() + 1, 34 'day': dateTime.getDate() 35 } 36 return result 37 } 38 39 getCalendarDate(): {} { 40 let dateTime = new Date(); 41 let res = { 42 'calendarYear': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarYear, 43 'calendarMonth': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarMonth, 44 'calendarDay': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarDay 45 } 46 return res 47 } 48 49 getSystemWeek() { 50 let dateTime = new Date(); 51 let days = dateTime.getDay(); 52 let week = this.convert(days) 53 return week 54 } 55 56 concatTime(hours, minutes): string{ 57 return `${this.fill(hours)}:${this.fill(minutes)}`; 58 }; 59 60 fill(value) { 61 return (value > 9 ? "" : "0") + value; 62 }; 63 64 convert(days) { 65 switch (days) { 66 case 0: 67 return $r('app.string.sunday'); 68 break; 69 case 1: 70 return $r('app.string.monday'); 71 break; 72 case 2: 73 return $r('app.string.tuesday'); 74 break; 75 case 3: 76 return $r('app.string.wednesday'); 77 break; 78 case 4: 79 return $r('app.string.thursday'); 80 break; 81 case 5: 82 return $r('app.string.friday'); 83 break; 84 case 6: 85 return $r('app.string.saturday'); 86 break; 87 default: 88 break; 89 } 90 } 91} 92 93let dateTimeCommon = new DateTimeCommon(); 94 95export default dateTimeCommon as DateTimeCommon 96