18779efd5Sopenharmony_ci/** 28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License. 58779efd5Sopenharmony_ci * You may obtain a copy of the License at 68779efd5Sopenharmony_ci * 78779efd5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88779efd5Sopenharmony_ci * 98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and 138779efd5Sopenharmony_ci * limitations under the License. 148779efd5Sopenharmony_ci */ 158779efd5Sopenharmony_ci 168779efd5Sopenharmony_ciimport i18n from '@ohos.i18n'; 178779efd5Sopenharmony_ci 188779efd5Sopenharmony_ciconst YEAR_CONVERT: number = 2697; 198779efd5Sopenharmony_ciconst YEAR_CYCLE: number = 60; 208779efd5Sopenharmony_ciconst LUNAR_MONTH = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊']; 218779efd5Sopenharmony_ciconst LUNAR_DAY = ['初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', 228779efd5Sopenharmony_ci'十五', '十六', '十七', '十八', '十九', '廿十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十', 238779efd5Sopenharmony_ci'三一']; 248779efd5Sopenharmony_ciconst NUM_CHAR = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; 258779efd5Sopenharmony_ci 268779efd5Sopenharmony_ciclass CalendarUtil { 278779efd5Sopenharmony_ci //公历转农历 288779efd5Sopenharmony_ci getLunarDate(date: Date) { 298779efd5Sopenharmony_ci let calendar = i18n.getCalendar('zh-CN', 'chinese'); 308779efd5Sopenharmony_ci calendar.setTime(date); 318779efd5Sopenharmony_ci const lunarMonth = calendar.get('month'); 328779efd5Sopenharmony_ci const lunarDay = calendar.get('date'); 338779efd5Sopenharmony_ci let lunarYear = calendar.get('era') * YEAR_CYCLE + calendar.get('year') - YEAR_CONVERT; 348779efd5Sopenharmony_ci return { 358779efd5Sopenharmony_ci year: lunarYear, 368779efd5Sopenharmony_ci month: lunarMonth, 378779efd5Sopenharmony_ci day: lunarDay 388779efd5Sopenharmony_ci } 398779efd5Sopenharmony_ci } 408779efd5Sopenharmony_ci 418779efd5Sopenharmony_ci formatLunarDate(lunar: any) { 428779efd5Sopenharmony_ci if (!lunar) { 438779efd5Sopenharmony_ci return ''; 448779efd5Sopenharmony_ci } 458779efd5Sopenharmony_ci let result: string = ''; 468779efd5Sopenharmony_ci if (lunar.year) { 478779efd5Sopenharmony_ci result += `${lunar.year}年` 488779efd5Sopenharmony_ci } 498779efd5Sopenharmony_ci if (lunar.month != undefined) { 508779efd5Sopenharmony_ci result += `${LUNAR_MONTH[lunar.month]}月` 518779efd5Sopenharmony_ci } 528779efd5Sopenharmony_ci if (lunar.day) { 538779efd5Sopenharmony_ci result += ` ${LUNAR_DAY[lunar.day - 1]}` 548779efd5Sopenharmony_ci } 558779efd5Sopenharmony_ci return result; 568779efd5Sopenharmony_ci } 578779efd5Sopenharmony_ci} 588779efd5Sopenharmony_ci 598779efd5Sopenharmony_ciexport default new CalendarUtil(); 608779efd5Sopenharmony_ci 618779efd5Sopenharmony_ci 628779efd5Sopenharmony_ci 638779efd5Sopenharmony_ci 64