Skip to content

Commit

Permalink
add birthday info
Browse files Browse the repository at this point in the history
  • Loading branch information
lmj01 committed Nov 12, 2023
1 parent 0c32a98 commit 0ff36e2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
4 changes: 4 additions & 0 deletions dev-note/bazel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# [bazel](https://bazel.google.cn/?hl=en)
> a fast, scalable, multi-language and extensible build system.
[github代码](https://github.com/bazelbuild/bazel)
62 changes: 62 additions & 0 deletions html/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const ChinaDay = ["\u65e5","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d"
// ['初','十','廿','卅','闰']
const ChinaElement = ["\u521d","\u5341","\u5eff","\u5345", "\u95f0"]



// 农历日中文显示,参数日期day
export const toChinaDay = function(day) {
let str = '';
Expand Down Expand Up @@ -93,6 +95,66 @@ export const leapDays = function(year) {
return 0;
}

/**
* 阳历月份天数
* 平年2月28天,闰年2月29天
* 4、6、9、11是30天
* 1、3、5、7、8、10、12是31天
*
*/
const SolarMonthDays = [0,31,28,31,30,31,30,60,31,31,30,31,30,31];
const isSolarLeap = (year) => {
return ((year %4) == 0 && (year%100) !== 0) || (year%100) === 0;
}
/**
* 农历转阳历,算同一年内两个月份的差异,以九月初三为例来说明
* 阳历 农历
* x-y 9-3
* 9-3 7-11
*
* @param y
* @param m
* @param d
*/
export const lunarToSolar = (y, m, d) => {
// 当年阳历对应的农历
const solar93 = solarToLunar(y, m, d);
// 当年农历的月份天数
const months = lunarMonthDays(y);
const dayList = [];
// 对应阳历的农历到目标农历的天数
// 当月天数
dayList.push(months[solar93.lunarM] - solar93.lunarD);
for (let i = solar93.lunarM + 1; i < m;i++) {
dayList.push(months[i]);
}
dayList.push(d);
// 因为阳历天数是固定的,算出来
// 假设的阳历加上这个天数差就是
let total = dayList.reduce((p,c)=>p+c,0);
let month = m, day = d;
for (let i = m; ;) {
const theMonthDays = SolarMonthDays[i];
// 第一个月
if (i === m && theMonthDays - d < total) {
total -= (theMonthDays - d - 1);
i++;
} else {
if (total > theMonthDays) {
total -= theMonthDays;
i++; // 下一个月
} else {
day = total;
month = i;
break;
}
}
}
return {
month, day
};
}

// 某年份农历各月天数
export const lunarMonthDays = function(year) {
year = year || nowInfo().y;
Expand Down
41 changes: 29 additions & 12 deletions html/personal.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ <h4 id="section1">生日</h4>
<label>幺儿生日<input id="input20220828" type="text" disabled value="2022-08-28"></label>
</div>
<div id="div19870903">
<label>自己生日<input id="input19870903" type="text" disabled value="1987-10-25"></label>
<label>自己生日<input id="input19870903" type="text" disabled value="1987-09-03"></label>
</div>
<div id="divOtherBirth">
<label>别人生日<input id="inputOtherBirth" type="text" placeholder="请输入年-月-日格式"></label>
<label>农历出生<input id="checkboxOtherBirth" type="checkbox" checked ></label>
<label>出生按农历<input id="checkboxOtherBirthType" type="checkbox" checked ></label>
<div id="divOtherInfo"></div>
</div>
</div>
</div>
<div></div>
<script type="module">
import { timeDay, timeYear, timeMonth } from "https://cdn.skypack.dev/d3-time@3";
import { solarToLunar, toChinaMonth, toChinaDay, lunarMonthDays, leapMonth } from './calendar.mjs';
import { solarToLunar, toChinaMonth, toChinaDay, lunarMonthDays, leapMonth, lunarToSolar } from './calendar.mjs';
const div19960124 = document.getElementById('div19960124');
const div20220828 = document.getElementById('div20220828');
const div19870903 = document.getElementById('div19870903');
Expand All @@ -43,21 +45,34 @@ <h4 id="section1">生日</h4>
const birthSelf = new Date(document.getElementById('input19870903').value);
const today = new Date();

function computeTimeDate(el, date, birthDayUseChineseCalendar = true) {
function computeTimeDate(el, date, isChineseCalendar = true, doBirthInChinsesCalendar = true) {
if (['divOtherInfo'].includes(el.id)) {
el.replaceChildren();
}
const birthYear = date.getFullYear(), birthMonth = date.getMonth() + 1, birthDay = date.getDate();
// 出生农历年
const chineseCalendar = solarToLunar(date.getFullYear(), date.getMonth()+1, date.getDate());
const infoChineseCalendar = `农历${chineseCalendar.lunarY}${toChinaMonth(chineseCalendar.lunarM, chineseCalendar.isLeap)}${toChinaDay(chineseCalendar.lunarD)}`;
const chineseCalendar = solarToLunar(birthYear, birthMonth, birthDay);
// 算对立的出生日期
let anotherBirthday = '', date2;
if (isChineseCalendar) {
anotherBirthday = `农历${toChinaMonth(chineseCalendar.lunarM, chineseCalendar.isLeap)}${toChinaDay(chineseCalendar.lunarD)}`;
} else {
// 从农历算阳历,到差天数来算
const solarCalendar = lunarToSolar(birthYear, birthMonth, birthDay);
console.log(solarCalendar)
date2 = new Date(birthYear,solarCalendar.month, solarCalendar.day)
anotherBirthday = `阳历${solarCalendar.month}${solarCalendar.day}日`;
}
let p = document.createElement('p');
p.classList.add('ms-3');
const totalYear = timeYear.count(date, today);
const totalMonth = timeMonth.count(date, today);
p.innerText = `出生于${infoChineseCalendar}、距今已有${totalYear}${totalMonth - 12 * totalYear}月、成长天数为${timeDay.count(date, today)}`;
// 当月过生日那天的日期与现在天数大小
const havingPassedDay = today.getDate() < date.getDate();
p.innerText = `出生于${anotherBirthday}、虚岁${totalYear}${totalMonth - 12 * totalYear - (!havingPassedDay ? 0 : 1)}月、成长天数为${timeDay.count(isChineseCalendar?date:date2, today)}`;
el.appendChild(p);
p = p.cloneNode(true);
if (birthDayUseChineseCalendar) {
if (doBirthInChinsesCalendar) {
const thisChineseCalendar = solarToLunar(today.getFullYear(), today.getMonth()+1, today.getDate());
const thisYear = today.getFullYear();
const thisMonth = chineseCalendar.lunarM;
Expand Down Expand Up @@ -90,17 +105,19 @@ <h4 id="section1">生日</h4>
dayList.push(chineseCalendar.lunarD);
p.innerText = `下个农历生日还有${dayList.reduce((p,c)=>p+c, 0) - 1}天到来`;
} else {
const nextBirthDay = new Date(today.getFullYear()+1, date.getMonth(), date.getDate());
const nextBirthDay = new Date(today.getFullYear()+1, birthMonth-1, birthDay);
p.innerText = `下个阳历生日还有${timeDay.count(today, nextBirthDay)}天到来`;
}
el.appendChild(p);
}
computeTimeDate(div20220828, birthSon, false);
computeTimeDate(div19960124, birthDarling);
computeTimeDate(div19870903, birthSelf);
computeTimeDate(div20220828, birthSon, true, false);
computeTimeDate(div19960124, birthDarling, true, true);
computeTimeDate(div19870903, birthSelf, false, true);
document.getElementById('inputOtherBirth').addEventListener('change', (event)=>{
const otherBirth = event.target.value;
if (otherBirth) computeTimeDate(divOtherInfo, new Date(otherBirth));
if (otherBirth) computeTimeDate(divOtherInfo, new Date(otherBirth),
document.getElementById('checkboxOtherBirth').checked,
document.getElementById('checkboxOtherBirth').checked);
})
</script>
</div>
Expand Down
2 changes: 2 additions & 0 deletions index/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
- [wsl](../dev-note/wsl.md)
- [vscode](../dev-note/vscode.md)
- [cmd](../dev-note/cmd.md)
- [bazel](../dev-note/bazel.md)
- [Mathjax](../articles/mathjax.md)
- [app 2023](../articles/2023/app.md)


### web
- [网页开发内容](../web/index.md)
- [webassembly](../web/webAssembly.md)
Expand Down
1 change: 0 additions & 1 deletion index/online.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

## 工具


### 文档

- [PubScholar公益学术平台](https://pubscholar.cn/)
Expand Down

0 comments on commit 0ff36e2

Please sign in to comment.