Giter Site home page Giter Site logo

Comments (6)

Lofanmi avatar Lofanmi commented on May 27, 2024

@zhangyiming748 希望对你有帮助,烦请右上角 star 一下哈哈 :)

package main

import (
	"fmt"
	"time"

	"github.com/Lofanmi/chinese-calendar-golang/lunar"
)

func main() {
	// 本地使用北京时区
	loc, _ := time.LoadLocation("Asia/Shanghai")
	time.Local = loc

	// 农历:二零二一年十一月初三
	t := time.Unix(lunar.ToSolarTimestamp(2021, 11, 3, 0, 0, 0, false), 0)
	// 公历:2021-12-06 00:00:00
	fmt.Println(t.Format("公历:2006-01-02 15:04:05"))
	// 和今天相差多少天
	fmt.Println(int(time.Now().Sub(t).Hours() / 24)) // 5天(算出来要除以24小时)

	// 如果是农历,计算两个农历相差多少天,都转成 time.Time 再 Sub 就好了 :)
	// 农历:二零二一年十二月初三
	t2 := time.Unix(lunar.ToSolarTimestamp(2021, 12, 3, 0, 0, 0, false), 0)
	// 公历:2022-01-05 00:00:00
	fmt.Println(t2.Format("公历:2006-01-02 15:04:05"))
	// 相差多少天
	fmt.Println(int(t2.Sub(t).Hours() / 24)) // 30天
}

from chinese-calendar-golang.

zhangyiming748 avatar zhangyiming748 commented on May 27, 2024

github.com/zhangyiming748/calendar/[email protected]
不过还是感谢你给我提供了一个思路
我现在的问题是
跨年的农历日期没法计算
我还在想办法
star点了

from chinese-calendar-golang.

Lofanmi avatar Lofanmi commented on May 27, 2024

如果都可以转换成time.Time,就可以直接相减了。

from chinese-calendar-golang.

zhangyiming748 avatar zhangyiming748 commented on May 27, 2024

如果都可以转换成time.Time,就可以直接相减了。

我自己写了一个代码,现在面临的问题是如果已经过了今年的当前农历日期,增加一年,但是我直接加365(366)天,得到的结果好像不太对,您有什么好办法么

from chinese-calendar-golang.

Lofanmi avatar Lofanmi commented on May 27, 2024

@zhangyiming748 你可以贴下代码,我看下?

from chinese-calendar-golang.

zhangyiming748 avatar zhangyiming748 commented on May 27, 2024

@zhangyiming748 你可以贴下代码,我看下?

func allInLuna(date string) int {
	day := strings.Join([]string{thisYear(), date}, "-")
	convert := solarlunar.LunarToSolar(day, false)
	ret, _ := time.Parse("2006-01-02", convert)
	unsub := ret.Sub(time.Now())
	if unsub < 0 {
		return int(unsub.Hours())/24 + Year
	}
	return int(unsub.Hours()) / 24
}

我现在用的这种方法

from chinese-calendar-golang.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.