Comments (6)
@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.
github.com/zhangyiming748/calendar/[email protected]
不过还是感谢你给我提供了一个思路
我现在的问题是
跨年的农历日期没法计算
我还在想办法
star点了
from chinese-calendar-golang.
如果都可以转换成time.Time,就可以直接相减了。
from chinese-calendar-golang.
如果都可以转换成time.Time,就可以直接相减了。
我自己写了一个代码,现在面临的问题是如果已经过了今年的当前农历日期,增加一年,但是我直接加365(366)天,得到的结果好像不太对,您有什么好办法么
from chinese-calendar-golang.
@zhangyiming748 你可以贴下代码,我看下?
from chinese-calendar-golang.
@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)
- 有个寿星万年历, js版,开源的, 可以参考 HOT 4
- 农历只支持到2100年 HOT 3
- 农历算法有错误 HOT 1
- 缺少时辰的返回 HOT 4
- 支持的年份太少了 HOT 1
- Implement the algorithm for the lunar calendar
- Lunar Calendar error for 2024 HOT 1
- 老弟, 星座不准啊。 HOT 2
- 轉換錯誤 HOT 1
- 2033问题出来了 HOT 3
- 还在维护吗? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chinese-calendar-golang.