This content originally appeared on DEV Community and was authored by xiaoyaolihua
In the last article, we finished the basic rendering of the calendar. Next, we began to internationalize the calendar.
- Calendar, successfully completed the acquisition of lunar calendar date.
The sample code is as follows:
import { i18n } from '@kit.LocalizationKit';
@Entry
@Component
struct Index {
private scrollerForScroll: Scroller = new Scroller();
private scrollerForList: Scroller = new Scroller();
@State listPosition: number = 0; // 0 means scrolling to the top of the List, 1 means intermediate value, and 2 means scrolling to the bottom of the List.
//Design lunar calendar time
getLunarDate(year: number, month: number, day: number) {
// 1. Set Gregorian calendar date
const gregorianCalendar = i18n.getCalendar("zh-Hans", "gregory");
gregorianCalendar.set(year, month - 1, day); // 月份需减1
// 2. Convert to lunar calendar
const lunarCalendar = i18n.getCalendar("zh-Hans", "chinese");
lunarCalendar.setTime(gregorianCalendar.getTimeInMillis());
// 3. Get the lunar calendar value
const lunarYear = lunarCalendar.get("year");
const lunarMonth = lunarCalendar.get("month");
const lunarDay = lunarCalendar.get("date");
// 4. Convert to Chinese lunar calendar
const lunarMonths = ['New Year', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'Winter Moon', 'Wax Moon'];
const lunarDays = ['Lunar 1st', '2nd Lunar Month', '3rd Lunar Synchronous', '4th Lunar Synchronous', '5th Lunar Attainment', 'Lunar 6', 'Lunar 7', 'Lunar 8', 'Lunar 9', 'Lunar 10',
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty',
'Twenty-one', 'Twenty-two', 'Twenty-three', 'Twenty-four', 'Twenty-five', 'Twenty-six', 'Twenty-Twenty-Seven', 'Twenty-Eight', 'Twenty-Nine', 'Thirty'];
}
getTime(index: number = 0) {
let arr: string[] = []
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1 + index;
const day = date.getDate();
const week = date.getDay();
// Example: Get the day of the week on Thursday, June 5, 2025
const targetDate = new Date(year, month - 1, 1); // Note: Months are counted from 0 (5 for June)
const dayIndex = targetDate.getDay(); // Back 4 (Thursday)
const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"
, "Friday", "Saturday"];
for (let i = 0; i < dayIndex; i++) {
arr.push('')
}
// Get how many days there are in a month
const days = new Date(year, month, 0).getDate();
for (let i = 1; i < days + 1; i++) {
arr.push(i + '')
}
return arr
}
aboutToAppear(): void {
this.getTime()
this.getLunarDate(2025, 6, 7)
}
build() {
Column() {
// Nav head navigation bar
Row() {
Image($r('sys.media.ohos_ic_public_arrow_left'))
.width(28)
.aspectRatio(1)
.fillColor(Color.Black)
.onClick(() => {
})
Text('Calendar')
.fontSize(18)
Column()
.width(28)
}
.padding(8)
.width('100%')
.backgroundColor('#50cccccc')
.justifyContent(FlexAlign.SpaceBetween)
// Date arrangement
Row() {
Text('Sunday')
.fontColor('#FF4A24')
Text('Monday')
Text('Tuesday')
Text('Wednesday')
Text('Thursday')
Text('Friday')
Text('Saturday')
.fontColor('#FF4A24')
}
.width('100%')
.justifyContent(FlexAlign.SpaceAround)
.padding(8)
//Scroll through the calendar
Scroll(this.scrollerForScroll) {
Column() {
CalenderComp({ arr: this.getTime(), index: 0 })
CalenderComp({ arr: this.getTime(1), index: 1 })
CalenderComp({ arr: this.getTime(2), index: 2 })
}
}
.width("100%")
.layoutWeight(1)
}
.width('100%')
.layoutWeight(1)
}
}
@Component
struct CalenderComp {
@prop arr: string[] = []
@prop index: number = 0
year: number = 0
month: number = 0
aboutToAppear(): void {
this.year = (new Date()).getFullYear();
this.month = (new Date()).getMonth() + 1 + this.index
}
build() {
Column({ space: 5 }) {
Text(this.year + 'year' + this.month + 'month')
.fontSize(18)
.width('100%')
.padding(8)
.textAlign(TextAlign.Center)
// dividingLine
Divider()
.color(Color.Red)
.strokeWidth(2)
.margin({ top: 1, bottom: 10 })
Grid() {
ForEach(this.arr, (item: number) => {
GridItem() {
Text(item.toString())
.fontSize(16)
.textAlign(TextAlign.Center)
}
})
}
.columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
}
}
}
- Calendar, the interface shows the lunar calendar.
The sample code is as follows:
import { i18n } from '@kit.LocalizationKit';
interface CalenderType {
Calendar?: string;
Lunar?: string;
}
@Entry
@Component
struct Index {
private scrollerForScroll: Scroller = new Scroller();
private scrollerForList: Scroller = new Scroller();
@State listPosition: number = 0; // 0 means scrolling to the top of the List, 1 means intermediate value, and 2 means scrolling to the bottom of the List.
//Design lunar calendar time
getLunarDate(year: number, month: number, day: number) {
// 1. Set Gregorian calendar date
const gregorianCalendar = i18n.getCalendar("zh-Hans", "gregory");
gregorianCalendar.set(year, month - 1, day); // 月份需减1
// 2. Convert to lunar calendar
const lunarCalendar = i18n.getCalendar("zh-Hans", "chinese");
lunarCalendar.setTime(gregorianCalendar.getTimeInMillis());
// 3. Get the lunar calendar value
const lunarYear = lunarCalendar.get("year");
const lunarMonth = lunarCalendar.get("month");
const lunarDay = lunarCalendar.get("date");
// 4. Convert to Chinese lunar calendar
const lunarMonths = ['New Year', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'Winter Moon', 'Wax Moon'];
const lunarDays = ['Lunar 1st', '2nd Lunar Month', '3rd Lunar Synchronous', '4th Lunar Synchronous', '5th Lunar Attainment', 'Lunar 6', 'Lunar 7', 'Lunar 8', 'Lunar 9', 'Lunar 10',
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty',
'Twenty-one', 'Twenty-two', 'Twenty-three', 'Twenty-four', 'Twenty-five', 'Twenty-six', 'Twenty-Twenty-Seven', 'Twenty-Eight', 'Twenty-Nine', 'Thirty'];
return lunarDays[lunarDay - 1]
}
getTime(index: number = 0) {
let arr: CalenderType[] = []
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1 + index;
const day = date.getDate();
const week = date.getDay();
// Example: Get the day of the week on Thursday, June 5, 2025
const targetDate = new Date(year, month - 1, 1); // Note: Months are counted from 0 (5 for June)
const dayIndex = targetDate.getDay(); // Back 4 (Thursday)
const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"
, "Friday", "Saturday"];
for (let i = 0; i < dayIndex; i++) {
arr.push({ })
}
// Get how many days there are in a month
const days = new Date(year, month, 0).getDate();
for (let i = 1; i < days + 1; i++) {
arr.push({ Calendar: i + '', Lunar: this.getLunarDate(year, month, i) })
}
return arr
}
aboutToAppear(): void {
this.getTime()
this.getLunarDate(2025, 6, 7)
}
build() {
Column() {
// Nav head navigation bar
Row() {
Image($r('sys.media.ohos_ic_public_arrow_left'))
.width(28)
.aspectRatio(1)
.fillColor(Color.Black)
.onClick(() => {
})
Text('Calendar')
.fontSize(18)
Column()
.width(28)
}
.padding(8)
.width('100%')
.backgroundColor('#50cccccc')
.justifyContent(FlexAlign.SpaceBetween)
// Date arrangement
Row() {
Text('Sunday')
.fontColor('#FF4A24')
.layoutWeight(1)
.fontSize(12)
Text('Monday')
.layoutWeight(1)
.fontSize(12)
Text('Tuesday')
.layoutWeight(1)
.fontSize(12)
Text('Wednesday')
.layoutWeight(1)
.fontSize(12)
Text('Thursday')
.fontSize(12)
.layoutWeight(1)
Text('Friday')
.fontSize(12)
.layoutWeight(1)
Text('Saturday')
.fontSize(12)
.layoutWeight(1)
.fontColor('#FF4A24')
}
.width('100%')
.justifyContent(FlexAlign.SpaceAround)
.padding(8)
//Scroll through the calendar
Scroll(this.scrollerForScroll) {
Column() {
CalenderComp({ arr: this.getTime(), index: 0 })
CalenderComp({ arr: this.getTime(1), index: 1 })
CalenderComp({ arr: this.getTime(2), index: 2 })
}
}
.width("100%")
.layoutWeight(1)
}
.width('100%')
.layoutWeight(1)
}
}
@Component
struct CalenderComp {
@prop arr: CalenderType[] = []
@prop index: number = 0
year: number = 0
month: number = 0
aboutToAppear(): void {
this.year = (new Date()).getFullYear();
this.month = (new Date()).getMonth() + 1 + this.index
}
build() {
Column({ space: 5 }) {
Text(this.year + 'year' + this.month + 'month')
.fontSize(18)
.width('100%')
.padding(8)
.textAlign(TextAlign.Center)
// dividingLine
Divider()
.color(Color.Red)
.strokeWidth(2)
.margin({ top: 1, bottom: 10 })
Grid() {
ForEach(this.arr, (item: CalenderType) => {
GridItem() {
Column() {
Text(item.Calendar)
.fontSize(16)
.textAlign(TextAlign.Center)
Text(item.Lunar)
.fontSize(16)
.textAlign(TextAlign.Center)
}
}
})
}
.columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
}
}
}
This content originally appeared on DEV Community and was authored by xiaoyaolihua

xiaoyaolihua | Sciencx (2025-06-29T14:46:50+00:00) HarmonyOS NEXT- project actual combat calendar, novice tutorial 2. Retrieved from https://www.scien.cx/2025/06/29/harmonyos-next-project-actual-combat-calendar-novice-tutorial-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.