lunar

package
v2.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 4 Imported by: 1

README

Chinese Lunar

English | 简体中文 | 日本語

Usage and example

Currently only 200 years from 1900 to 2100 are supported

Convert Gregorian calendar to Lunar calendar
// Get Lunar year of animal
carbon.Parse("2020-08-05 13:14:15").Lunar().Animal() // 鼠
// Get lunar festival
carbon.Parse("2021-02-12").Lunar().Festival() // 春节

// Get lunar year
carbon.Parse("2020-08-05").Lunar().Year() // 2020
// Get lunar month
carbon.Parse("2020-08-05").Lunar().Month() // 6
// Get lunar leap month
carbon.Parse("2020-08-05").Lunar().LeapMonth() // 4
// Get lunar day
carbon.Parse("2020-08-05").Lunar().Day() // 16
// Get lunar hour
carbon.Parse("2020-08-05").Lunar().Hour() // 13
// Get lunar minute
carbon.Parse("2020-08-05").Lunar().Minute() // 14
// Get lunar second
carbon.Parse("2020-08-05").Lunar().Second() // 15

// Get lunar date and time string
carbon.Parse("2020-08-05").Lunar().String() // 2020-06-16
fmt.Printf("%s", carbon.Parse("2020-08-05").Lunar()) // 2020-06-16
// Get lunar year as string
carbon.Parse("2020-08-05").Lunar().ToYearString() // 二零二零
// Get lunar month as string
carbon.Parse("2020-08-05").Lunar().ToMonthString() // 六月
// Get lunar leap month as string
carbon.Parse("2020-04-23").Lunar().ToMonthString() // 闰四月
// Get lunar week as string
carbon.Parse("2020-04-23").Lunar().ToWeekString() // 周四
// Get lunar day as string
carbon.Parse("2020-08-05").Lunar().ToDayString() // 十六
// Get lunar date as string
carbon.Parse("2020-08-05").Lunar().ToDateString() // 二零二零年六月十六

Convert Lunar calendar to Gregorian calendar
// Convert the Lunar Calendar December 11, 2023 to the gregorian calendar
carbon.CreateFromLunar(2023, 12, 11, 0, 0, 0, false).ToDateTimeString() // 2024-01-21 00:00:00
// Convert lunar calendar February 11, 2023 to gregorian calendar
carbon.CreateFromLunar(2023, 2, 11, 0, 0, 0, false).ToDateTimeString() // 2024-03-02 00:00:00
// Convert the Lunar Calendar Leap February 11, 2024 to the gregorian calendar
carbon.CreateFromLunar(2023, 2, 11, 0, 0, 0, true).ToDateTimeString() // 2023-04-01 00:00:00
Comparison
// Whether is a valid lunar date
carbon.Parse("0000-00-00").Lunar().IsValid() // false
carbon.Parse("2020-08-05").Lunar().IsValid() // true

// Whether is a lunar leap year
carbon.Parse("2020-08-05").Lunar().IsLeapYear() // true
// Whether is a lunar leap month
carbon.Parse("2020-08-05").Lunar().IsLeapMonth() // false

// Whether is a lunar year of the rat
carbon.Parse("2020-08-05").Lunar().IsRatYear() // true
// Whether is a lunar year of the ox
carbon.Parse("2020-08-05").Lunar().IsOxYear() // false
// Whether is a lunar year of the tiger
carbon.Parse("2020-08-05").Lunar().IsTigerYear() // false
// Whether is a lunar year of the rabbit
carbon.Parse("2020-08-05").Lunar().IsRabbitYear() // false
// Whether is a lunar year of the dragon
carbon.Parse("2020-08-05").Lunar().IsDragonYear() // false
// Whether is a lunar year of the snake
carbon.Parse("2020-08-05").Lunar().IsSnakeYear() // false
// Whether is a lunar year of the horse
carbon.Parse("2020-08-05").Lunar().IsHorseYear() // false
// Whether is a lunar year of the goat
carbon.Parse("2020-08-05").Lunar().IsGoatYear() // false
// Whether is a lunar year of the monkey
carbon.Parse("2020-08-05").Lunar().IsMonkeyYear() // false
// Whether is a lunar year of the rooster
carbon.Parse("2020-08-05").Lunar().IsRoosterYear() // false
// Whether is a lunar year of the dog
carbon.Parse("2020-08-05").Lunar().IsDogYear() // false
// Whether is a lunar year of the dig
carbon.Parse("2020-08-05").Lunar().IsPigYear() // false

Documentation

Overview

Package lunar is part of the carbon package.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLunar = func() error {
	return fmt.Errorf("invalid lunar date, please make sure the lunar date is valid")
}

ErrInvalidLunar returns a invalid lunar date. 无效的农历日期错误

Functions

This section is empty.

Types

type Lunar

type Lunar struct {
	Error error
	// contains filtered or unexported fields
}

Lunar defines a Lunar struct. 定义 Lunar 结构体

func FromStdTime added in v2.6.2

func FromStdTime(t time.Time) *Lunar

FromStdTime creates a Lunar instance from standard time.Time. 从标准 time.Time 创建 Lunar 实例

func MaxValue added in v2.5.2

func MaxValue() *Lunar

MaxValue returns a Lunar instance for the greatest supported date. 返回 Carbon 的最大值

func MinValue added in v2.5.2

func MinValue() *Lunar

MinValue returns a Lunar instance for the lowest supported date. 返回 Lunar 的最小值

func NewLunar added in v2.6.2

func NewLunar(year, month, day int, isLeapMonth bool) *Lunar

NewLunar returns a new Lunar instance. 返回 Lunar 实例

func (*Lunar) Animal

func (l *Lunar) Animal() string

Animal gets lunar animal name like "猴". 获取农历生肖

func (*Lunar) Day

func (l *Lunar) Day() int

Day gets lunar day like 5. 获取农历日,如 5

func (*Lunar) Festival

func (l *Lunar) Festival() string

Festival gets lunar festival name like "春节". 获取农历节日

func (*Lunar) IsDogYear

func (l *Lunar) IsDogYear() bool

IsDogYear reports whether is lunar year of Dog. 是否是狗年

func (*Lunar) IsDragonYear

func (l *Lunar) IsDragonYear() bool

IsDragonYear reports whether is lunar year of Dragon. 是否是龙年

func (*Lunar) IsGoatYear

func (l *Lunar) IsGoatYear() bool

IsGoatYear reports whether is lunar year of Goat. 是否是羊年

func (*Lunar) IsHorseYear

func (l *Lunar) IsHorseYear() bool

IsHorseYear reports whether is lunar year of Horse. 是否是马年

func (*Lunar) IsLeapMonth

func (l *Lunar) IsLeapMonth() bool

IsLeapMonth reports whether is a lunar leap month. 是否是农历闰月

func (*Lunar) IsLeapYear

func (l *Lunar) IsLeapYear() bool

IsLeapYear reports whether is a lunar leap year. 是否是农历闰年

func (*Lunar) IsMonkeyYear

func (l *Lunar) IsMonkeyYear() bool

IsMonkeyYear reports whether is lunar year of Monkey. 是否是猴年

func (*Lunar) IsOxYear

func (l *Lunar) IsOxYear() bool

IsOxYear reports whether is lunar year of Ox. 是否是牛年

func (*Lunar) IsPigYear

func (l *Lunar) IsPigYear() bool

IsPigYear reports whether is lunar year of Pig. 是否是猪年

func (*Lunar) IsRabbitYear

func (l *Lunar) IsRabbitYear() bool

IsRabbitYear reports whether is lunar year of Rabbit. 是否是兔年

func (*Lunar) IsRatYear

func (l *Lunar) IsRatYear() bool

IsRatYear reports whether is lunar year of Rat. 是否是鼠年

func (*Lunar) IsRoosterYear

func (l *Lunar) IsRoosterYear() bool

IsRoosterYear reports whether is lunar year of Rooster. 是否是鸡年

func (*Lunar) IsSnakeYear

func (l *Lunar) IsSnakeYear() bool

IsSnakeYear reports whether is lunar year of Snake. 是否是蛇年

func (*Lunar) IsTigerYear

func (l *Lunar) IsTigerYear() bool

IsTigerYear reports whether is lunar year of Tiger. 是否是虎年

func (*Lunar) IsValid added in v2.5.2

func (l *Lunar) IsValid() bool

IsValid reports whether is a valid lunar date. 是否是有效的年份

func (*Lunar) LeapMonth

func (l *Lunar) LeapMonth() int

LeapMonth gets lunar leap month like 2. 获取农历闰月月份,如 2

func (*Lunar) Month

func (l *Lunar) Month() int

Month gets lunar month like 8. 获取农历月份

func (*Lunar) String

func (l *Lunar) String() string

String implements Stringer interface for Lunar. 实现 Stringer 接口

func (*Lunar) ToDateString

func (l *Lunar) ToDateString() string

ToDateString outputs a string in lunar date format like "二零二零年腊月初五". 获取农历日期字符串,如 "二零二零年腊月初五"

func (*Lunar) ToDayString

func (l *Lunar) ToDayString() (day string)

ToDayString outputs a string in lunar day format like "廿一". 获取农历日字符串,如 "廿一"

func (*Lunar) ToGregorian

func (l *Lunar) ToGregorian(timezone ...string) *calendar.Gregorian

ToGregorian converts Lunar instance to Gregorian instance. 将 Lunar 实例转化为 Gregorian 实例

func (*Lunar) ToMonthString

func (l *Lunar) ToMonthString() (month string)

ToMonthString outputs a string in lunar month format like "正月". 获取农历月份字符串,如 "正月"

func (*Lunar) ToWeekString

func (l *Lunar) ToWeekString() (month string)

ToWeekString outputs a string in week layout like "周一". 输出完整农历星期字符串,如 "周一"

func (*Lunar) ToYearString

func (l *Lunar) ToYearString() (year string)

ToYearString outputs a string in lunar year format like "二零二零". 获取农历年份字符串,如 "二零二零"

func (*Lunar) Year

func (l *Lunar) Year() int

Year gets lunar year like 2020. 获取农历年份

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL