Giter VIP home page Giter VIP logo

tiny-lunar-calendar's Introduction

超迷你 JavaScript 農曆函式

這個函式可以取得 2001-2099 年所有農曆日期。以最緊湊的方式儲存農曆表,使得程式碼約佔1KB大小。

使用方式

let Y = getLunarCalendar(2020);

上例 Y 得一個二維陣列,存放2020年每一天農曆日期。第一維為陽曆的月,第二維為陽曆的日。每個數字表示農曆月日,個、十位數為農曆日,百位數為農曆月,若為負值表示農曆閏月。
看起來像這樣子:

[
  [1207, 1208, 1209, …],
  [108, 109, 110, …],
  [208, 209, 210, …],
  [309, 310, 311, …],
  [409, 410, 411, …],
  [-410, -411, -412, …],
  [511, 512, 513, …],
  …
  [1017, 1018, 1019, …]  
]

範例

  • 列出一整年農曆對照表
let y = 2020, Y = getLunarCalendar(y);

for(let m=0;m<12;m++){
  for(let d=0;d<Y[m].length;d++){
    let D = Y[m][d];
    console.log(`${y}/${m+1}/${d+1} 農曆${D<0?'閏':''}${Math.floor(Math.abs(D/100))}${Math.abs(D)%100}日`);
  }
}
  • 取得某年農曆春節的西元日期
console.log(new Date(2020,0,getLunarCalendar(2020).flat().indexOf(101)+1));

依上例類堆,求端午帶入505、求中秋帶入815。

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.