Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 无依赖获取阴历 #88

Open
AllanChain opened this issue May 21, 2020 · 0 comments
Open

JS 无依赖获取阴历 #88

AllanChain opened this issue May 21, 2020 · 0 comments
Labels
blog: programming @post This is a blog post tag: javascript |178272204-7e20ff98-56d1-457d-842f-70a4ecd17e0f.png

Comments

@AllanChain
Copy link
Owner

AllanChain commented May 21, 2020

View Post on Blog

This post was originally created at 2020-02-08T13:46:36+08:00

Intl 是个好东西,但是还不是很强大,浏览器支持上也有 Node 和 Android 的小缺憾。


Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl

const dtf = new Intl.DateTimeFormat('zh-hans-u-ca-chinese')
const gan = '甲乙丙丁戊己庚辛壬癸'
const zhi = '子丑寅卯辰巳午未申酉戌亥';
const yue = '正二三四五六七八九十冬腊';
const ri = ['初', '十', '廿', '三十'];
[year, month, date] = dtf.format(new Date()).split('/');
year--;
year = gan.charAt(year % 10) + zhi.charAt(year % 12);
let dates = '';
if (date % 10)
  dates = new Intl.NumberFormat('zh-hans-u-nu-hanidec').format(date % 10);
dates = ri[Math.floor(date / 10)] + dates;
console.log(`${year}${yue.charAt(month-1)}${dates}`);

日的表述还不够标准,就先这样了。

@AllanChain AllanChain added Gitalk and removed Gitalk labels May 21, 2020
@AllanChain AllanChain added @post This is a blog post blog: programming tag: javascript |178272204-7e20ff98-56d1-457d-842f-70a4ecd17e0f.png labels Jul 3, 2020
@AllanChain AllanChain changed the title JS 获取阴历(无需安装依赖) | AC's Blog JS 无依赖获取阴历 Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blog: programming @post This is a blog post tag: javascript |178272204-7e20ff98-56d1-457d-842f-70a4ecd17e0f.png
Projects
None yet
Development

No branches or pull requests

1 participant