HarmonyOS鸿蒙Next中闰年日期问题

HarmonyOS鸿蒙Next中闰年日期问题 在判断当前年份为闰年后,2月可以选29日

此时直接修改年份为非闰年,2月仍默认显示29日但实际上点开是没有29日的

怎么让我的年份变动时,直接把日月置为空值呢

4 回复

watch啊,去搜一下@watch怎么用

更多关于HarmonyOS鸿蒙Next中闰年日期问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


import { promptAction } from '@kit.ArkUI';

@Component
struct YMDPage {
  @State
  years: SelectOption[] = [];
  @State
  months: SelectOption[] = [];
  @State
  days: SelectOption[] = [];
  @State
  year: number = 0;
  @State
  month: number = 0;
  @State
  day: number = 0;
  birth = "";

  onPageShow(): void {
    for (let i = 1949; i <= new Date().getFullYear(); i++) {
      this.years.push({ value: `${i}` });
    }
  }

  private loadDays() {
    this.days = [];
    switch (this.month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        for (let i = 1; i <= 31; i++) {
          this.days.push({ value: `${i}` });
        }
        break;
      case 4:
      case 6:
      case 9:
      case 11:
        for (let i = 1; i <= 30; i++) {
          this.days.push({ value: `${i}` });
        }
        break;
      case 2:
        for (let i = 1; i <= 28; i++) {
          this.days.push({ value: `${i}` });
        }
        if (this.year % 4 == 0) {
          this.days.push({ value: `29` });
        }
        break;
    }
  }

  build() {
    Column() {
      Row() {
        Text("生日")
          .fontSize(13)
          .layoutWeight(2)
        Row() {
          Select(this.years)
            .layoutWeight(3)
            .scale({
              x: 0.8,
              y: 0.8
            })
            .borderRadius(10)
            .value(this.year ? `${this.year}` : "            年")
            .selected(this.year - 1949)
            .font({
              size: 17
            })
            .space(3)
            .optionHeight(300)
            .onSelect((index: number, year: string) => {
              this.year = Number(year);
              this.birth = `${this.year}-${this.month < 10 ? "0" + this.month : this.month}-${this.day < 10 ? "0" + this.day : this.day}`;
              if (this.month) {
                this.loadDays();
              }
              if (this.months.length) {
                return;
              }
              for (let i = 1; i <= 12; i++) {
                this.months.push({ value: `${i}` });
              }
            })
            .offset({
              x: -11
            })
          Select(this.months)
            .layoutWeight(2)
            .scale({
              x: 0.8,
              y: 0.8
            })
            .borderRadius(10)
            .value(this.month ? `${this.month}` : "   月")
            .font({
              size: 17
            })
            .selected(this.month - 1)
            .space(3)
            .optionHeight(300)
            .onClick(() => {
              if (this.year == 0) {
                promptAction.showToast({ message: "请先选择年份!", alignment: Alignment.Center });
              }
            })
            .onSelect((index: number, month: string) => {
              this.month = Number(month);
              this.birth = `${this.year}-${this.month < 10 ? "0" + this.month : this.month}-${this.day < 10 ? "0" + this.day : this.day}`;
              this.loadDays();
            })
            .offset({
              x: -5
            })
          Select(this.days)
            .layoutWeight(2)
            .scale({
              x: 0.8,
              y: 0.8
            })
            .borderRadius(10)
            .value(this.day ? `${this.day}` : "   日")
            .selected(this.day - 1)
            .font({
              size: 17
            })
            .space(3)
            .optionHeight(300)
            .onClick(() => {
              if (this.month == 0) {
                promptAction.showToast({ message: "请先选择月份!", alignment: Alignment.Center });
              }
            })
            .onSelect((index: number, day: string) => {
              this.day = Number(day);
              this.birth = `${this.year}-${this.month < 10 ? "0" + this.month : this.month}-${this.day < 10 ? "0" + this.day : this.day}`;
            })
            .offset({
              x: 5
            })
        }
        .layoutWeight(9)
      }

      Row() {
        Button("console")
          .onClick(() => {
            console.log(`${this.year},${this.month},${this.day}`)
          })
      }
    }
    .height('100%')
    .width('100%')
  }
}

在HarmonyOS鸿蒙Next中,处理闰年日期问题时,系统会遵循公历闰年规则。闰年判定规则如下:

  1. 能被4整除但不能被100整除的年份是闰年。
  2. 能被400整除的年份也是闰年。

鸿蒙Next的日期处理模块会自动根据上述规则判断闰年,并在日期计算中正确处理2月的天数。例如,2024年是闰年,2月有29天;而2023年不是闰年,2月有28天。系统API(如DateCalendar类)会确保日期操作的准确性。

在HarmonyOS鸿蒙Next中处理闰年日期问题时,需遵循以下规则:闰年能被4整除但不能被100整除,或能被400整除。例如,2000年是闰年,而1900年不是。开发者可使用Calendar类或LocalDate类进行日期操作,并通过条件判断验证闰年。示例代码:

import java.time.LocalDate;

public class LeapYearCheck {
    public static void main(String[] args) {
        int year = 2024;
        LocalDate date = LocalDate.of(year, 1, 1);
        boolean isLeap = date.isLeapYear();
        System.out.println(year + " is leap year: " + isLeap);
    }
}

此代码将输出2024 is leap year: true,表明2024年是闰年。

回到顶部