Showing
3 changed files
with
161 additions
and
4 deletions
| 1 | import { StringUtils } from './StringUtils'; | 1 | import { StringUtils } from './StringUtils'; |
| 2 | - | 2 | +import getLunar from './GetLunar' |
| 3 | /** | 3 | /** |
| 4 | * 日期/时间工具 | 4 | * 日期/时间工具 |
| 5 | */ | 5 | */ |
| @@ -457,6 +457,15 @@ export class DateTimeUtils { | @@ -457,6 +457,15 @@ export class DateTimeUtils { | ||
| 457 | return timeStr; | 457 | return timeStr; |
| 458 | } | 458 | } |
| 459 | 459 | ||
| 460 | + /** | ||
| 461 | + * 获取农历日期 | ||
| 462 | + * @param _date eg: '2024-02-01 12:12:12' or '2024-02-01' 如果不传,取当前时间 | ||
| 463 | + * @returns eg: '二月廿五' | ||
| 464 | + * 如果后面有其他农历需求,可以引入农历插件。这个引入的getLunar文件可以删除 | ||
| 465 | + */ | ||
| 466 | + static getLunar(_date?: string) { | ||
| 467 | + return getLunar(_date) | ||
| 468 | + } | ||
| 460 | } | 469 | } |
| 461 | 470 | ||
| 462 | // const dateTimeUtils = new DateTimeUtils() | 471 | // const dateTimeUtils = new DateTimeUtils() |
| 1 | +/** | ||
| 2 | + * 获取某个日期的农历 | ||
| 3 | + * @parmas {newDate} 日期 年-月-日 | ||
| 4 | + */ | ||
| 5 | +export default function getLunar(newDate){ | ||
| 6 | + var nyear; | ||
| 7 | + var nmonth; | ||
| 8 | + var nday = -1; | ||
| 9 | + var nwday; | ||
| 10 | + var nhrs; | ||
| 11 | + var nmin; | ||
| 12 | + var nsec; | ||
| 13 | + var newDate = newDate; | ||
| 14 | + | ||
| 15 | + var lmonth, lday, lleap; //农历参数 | ||
| 16 | + | ||
| 17 | + function Draw() { | ||
| 18 | + NewTick(); | ||
| 19 | + | ||
| 20 | + //显示时间 | ||
| 21 | + var s = nyear + '年' + nmonth + '月' + nday + '日 ' + '星期' + cweekday(nwday) + ' ' + shapetime(nhrs, nmin, nsec); | ||
| 22 | + s += " 农历" + lmonth + "月" + lday; //农历 | ||
| 23 | + var lunar_month_day=lmonth + "月" + lday; | ||
| 24 | + return lunar_month_day; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + function NewTick() { | ||
| 29 | + console.warn('noww---', newDate) | ||
| 30 | + var noww = newDate ? new Date(newDate) : new Date(); | ||
| 31 | + if (noww.getDate() != nday) { | ||
| 32 | + nyear = noww.getFullYear(); | ||
| 33 | + nmonth = noww.getMonth() + 1; | ||
| 34 | + nwday = noww.getDay(); | ||
| 35 | + nday = noww.getDate(); | ||
| 36 | + | ||
| 37 | + getlunar(); //获取农历 | ||
| 38 | + } | ||
| 39 | + nhrs = noww.getHours(); | ||
| 40 | + nmin = noww.getMinutes(); | ||
| 41 | + nsec = noww.getSeconds(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + //辅助函数 | ||
| 46 | + var hzWeek = new Array("日", "一", "二", "三", "四", "五", "六", "日"); | ||
| 47 | + function cweekday(wday) { | ||
| 48 | + return hzWeek[wday]; | ||
| 49 | + } | ||
| 50 | + function shapetime(vhrs, vmin, vsec) { | ||
| 51 | + if (vsec <= 9) vsec = "0" + vsec; | ||
| 52 | + if (vmin <= 9) vmin = "0" + vmin; | ||
| 53 | + if (vhrs <= 9) vhrs = "0" + vhrs; | ||
| 54 | + return vhrs + ":" + vmin + ":" + vsec | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + //农历函数开始 | ||
| 58 | + var lunarInfo = new Array(0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x05ac0, 0x0ab60, 0x096d5, 0x092e0, //1990 | ||
| 59 | + 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63); | ||
| 60 | + function lYearDays(y) { | ||
| 61 | + var i, sum = 348; | ||
| 62 | + for (i = 0x8000; i > 0x8; i >>= 1) sum += (lunarInfo[y - 1900] & i) ? 1 : 0; | ||
| 63 | + return (sum + leapDays(y)); | ||
| 64 | + } | ||
| 65 | + function leapDays(y) { | ||
| 66 | + if (leapMonth(y)) return ((lunarInfo[y - 1900] & 0x10000) ? 30 : 29); | ||
| 67 | + else return (0); | ||
| 68 | + } | ||
| 69 | + function leapMonth(y) { | ||
| 70 | + return (lunarInfo[y - 1900] & 0xf); | ||
| 71 | + } | ||
| 72 | + function monthDays(y, m) { | ||
| 73 | + return ((lunarInfo[y - 1900] & (0x10000 >> m)) ? 30 : 29); | ||
| 74 | + } | ||
| 75 | + function Lunar(y, m, d) { | ||
| 76 | + var i, leap = 0, | ||
| 77 | + temp = 0; | ||
| 78 | + var offset = (Date.UTC(y, m, d) - Date.UTC(1900, 0, 31)) / 86400000; | ||
| 79 | + for (i = 1900; i < 2050 && offset > 0; i++) { | ||
| 80 | + temp = lYearDays(i); | ||
| 81 | + offset -= temp; | ||
| 82 | + } | ||
| 83 | + if (offset < 0) { | ||
| 84 | + offset += temp; | ||
| 85 | + i--; | ||
| 86 | + } | ||
| 87 | + this.year = i; | ||
| 88 | + leap = leapMonth(i); | ||
| 89 | + this.isLeap = false; | ||
| 90 | + for (i = 1; i < 13 && offset > 0; i++) { | ||
| 91 | + if (leap > 0 && i == (leap + 1) && this.isLeap == false) {--i; | ||
| 92 | + this.isLeap = true; | ||
| 93 | + temp = leapDays(this.year); | ||
| 94 | + } else { | ||
| 95 | + temp = monthDays(this.year, i); | ||
| 96 | + } | ||
| 97 | + if (this.isLeap == true && i == (leap + 1)) this.isLeap = false; | ||
| 98 | + offset -= temp; | ||
| 99 | + } | ||
| 100 | + if (offset == 0 && leap > 0 && i == leap + 1) if (this.isLeap) { | ||
| 101 | + this.isLeap = false; | ||
| 102 | + } else { | ||
| 103 | + this.isLeap = true; --i; | ||
| 104 | + } | ||
| 105 | + if (offset < 0) { | ||
| 106 | + offset += temp; --i; | ||
| 107 | + } | ||
| 108 | + this.month = i; | ||
| 109 | + this.day = offset + 1; | ||
| 110 | + } | ||
| 111 | + var nStr1 = new Array('', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'); | ||
| 112 | + var nStr2 = new Array('初', '十', '廿', '卅', '□'); | ||
| 113 | + function GetcDay(d) { | ||
| 114 | + var s; | ||
| 115 | + switch (d) { | ||
| 116 | + case 10: | ||
| 117 | + s = '初十'; | ||
| 118 | + break; | ||
| 119 | + case 20: | ||
| 120 | + s = '二十'; | ||
| 121 | + break; | ||
| 122 | + case 30: | ||
| 123 | + s = '三十'; | ||
| 124 | + break; | ||
| 125 | + default: | ||
| 126 | + s = nStr2[Math.floor(d / 10)]; | ||
| 127 | + s += nStr1[d % 10]; | ||
| 128 | + break; | ||
| 129 | + } | ||
| 130 | + return (s); | ||
| 131 | + } | ||
| 132 | + function GetcMon(m) { | ||
| 133 | + if (m == 1) return '正'; | ||
| 134 | + else return nStr1[m]; | ||
| 135 | + } | ||
| 136 | + function getlunar() { | ||
| 137 | + var lObj = new Lunar(nyear, nmonth - 1, nday); | ||
| 138 | + lmonth = GetcMon(lObj.month); | ||
| 139 | + lday = GetcDay(lObj.day); | ||
| 140 | + lleap = lObj.isLeap; | ||
| 141 | + if (lleap == 1) { | ||
| 142 | + lmonth = "闰" + lmonth; | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | + //农历函数结束 | ||
| 146 | + return Draw(); | ||
| 147 | +} |
| 1 | import { CommonConstants } from 'wdConstant/Index' | 1 | import { CommonConstants } from 'wdConstant/Index' |
| 2 | - | 2 | +import { DateTimeUtils } from 'wdKit'; |
| 3 | /** | 3 | /** |
| 4 | * 播报标题描述 | 4 | * 播报标题描述 |
| 5 | */ | 5 | */ |
| @@ -33,10 +33,11 @@ export struct RecommendTitle { | @@ -33,10 +33,11 @@ export struct RecommendTitle { | ||
| 33 | .margin({right: 8}) | 33 | .margin({right: 8}) |
| 34 | .alignSelf(ItemAlign.Start) | 34 | .alignSelf(ItemAlign.Start) |
| 35 | Column() { | 35 | Column() { |
| 36 | - Text('正月二十八') | 36 | + Text(DateTimeUtils.getLunar()) |
| 37 | .fontSize($r('app.float.font_size_14')) | 37 | .fontSize($r('app.float.font_size_14')) |
| 38 | .fontColor($r('app.color.color_222222')) | 38 | .fontColor($r('app.color.color_222222')) |
| 39 | - Text('02/28') | 39 | + .alignSelf(ItemAlign.Start) |
| 40 | + Text(DateTimeUtils.formatDate(new Date().getTime(), 'MM/dd')) | ||
| 40 | .fontSize($r('app.float.font_size_24')) | 41 | .fontSize($r('app.float.font_size_24')) |
| 41 | .fontColor($r('app.color.color_222222')) | 42 | .fontColor($r('app.color.color_222222')) |
| 42 | } | 43 | } |
-
Please register or login to post a comment