Conversion Between the Javascript lunar calendar and the Gregorian Calendar

Source: Internet
Author: User
Conversion Between the Javascript lunar calendar and the Gregorian Calendar
/** Usage * Lunar. toSolar (2016, 6, 3); converts the Lunar calendar into the Gregorian calendar * Lunar. toLunar (2016, 7, 6); convert the Gregorian calendar */var Lunar = {MIN_YEAR: 1891, MAX_YEAR: 2100, lunarInfo: [[21936, 9,], [6, 1, 30,965 6], [17,958, 21168 4], [27296, 6,], [,], [,], [,], [42352,], [,], [,], [,], [,], [,], [,], [,], [55632,], [, 4,], [,], [0, 22176, 39632], [, 2, 6, 53840], [54944, 1, 18872], [42160, 27968], [,], [,], [,], [,], [,], [38256,], [,], [,], [,], [,], [,], [,], [,], [,], [0, 2, 17,54432], [0, 59984, 11104, 54432], [22176, 13,968], [37584, 0], [, 2, 17,27808], [46416, 6, 42416, 12,21168], [8, 43344, 27296], [19296,], [,], [,], [,], [,], [,], [21088, 55632], [,], [,], [,], [,], [,], [,], [38608, 42192], [,], [,], [,], [,], [,], [,], [,], [46752,], [,], [,], [,], [,], [,], [,], [], [18864, 5,], [4, 42168, 27968, 17,43872], [38256, 6, 25776, 43872], [,], [,], [,], [,], [,], [,], [,], [, 5, 51552], [55888, 24, 54440], [22176, 29,43736], [0, 9680, 37584], [46240, 7,], [,], [,], [,], [,], [42416,], [,], [,], [,], [,], [,], [,], [,], [43312, 5, 44368], [,], [,], [,], [,], [,], [,], [,], [53856, 23200], [,], [,], [,], [,], [,], [,], [,], [3, 1, 23,27472], [0, 38608, 42192, 4, 54560], [22224, 30,42168], [43600, 17,42160], [44448, 6,], [, 26,46376, 18808], [25776, 15,59984], [0, 27424, 37600], [51552,], [,], [,], [, 2,], [,], [55888, 42704], [,], [,], [,], [,], [,], [,], [,], [46240,], [,], [,], [,], [,], [,], [,], [,], [21920, 21168], [,], [,], [,], [,], [,], [,], [,], [26928, 9,], [7, 29336, 44368], [42352,], [,], [,], [,], [,], [, 3,], [55968,], [,], [,], [,], [,], [,], [,], [,], [19168, 53584], [, 5,], [,], [,], [,], [,], [,], [54560, 9, 100], // whether to leap year isLeapYear: function (year) {return (year % 4 = 0 & year %! = 0) | (year % 400 = 0);}, // supports the annual lunarYear: function (year) {var gan = ['geng ', 'sin', 'taobao', 'dece', 'A', 'B', 'bing', 'ding', 'pente', 'jid'], zhi = ['shen ', 'you', 'yun', 'hai ', 'zi', 'ug', 'yin ', 'mao', 'chen ', 'Si ', 'wu', 'wei '], str = year. toString (). split (""); return gan [str [3] + zhi [year % 12];}, // zodiacYear: function (year) {var zodiac = ['Monkey ', 'chicken', 'Dog', 'pig', 'rat ', 'ox', 'tiger ', 'rabbit ', 'long', 'snake ', 'Ma', 'yang']; return zodiac [year % 12] ;}, // calendar month days // @ param year Gregorian calendar-year // @ param month Gregorian calendar-month solarMonthDays: function (year, month) {var FebDays = this. isLeapYear (year )? 29: 28; var monthHash = ['', 31, FebDays, 31, 30, 31, 30, 31, 31, 30, 31]; return monthHash [month] ;}, // number of days in the lunar month lunarMonthDays: function (year, month) {var monthData = this. lunarMonths (year); return monthData [month-1];}, // array of calendar month days lunarMonths: function (year) {var yearData = this. lunarInfo [year-this. MIN_YEAR]; var leapMonth = yearData [0]; var bit = (+ yearData [3]). toString (2); var Months = []; for (var I = 0; I <bit. length; I ++) {months [I] = bit. substr (I, 1) ;}for (var k = 0, len = 16-months. length; k <len; k ++) {months. unshift ('0');} months = months. slice (0, (leapMonth = 0? 12: 13); for (var I = 0; I <months. length; I ++) {months [I] = + months [I] + 29;} return months ;}, // number of days of the lunar calendar year // @ param year lunarYearDays: function (year) {var monthArray = this. lunarYearMonths (year); var len = monthArray. length; return (monthArray [len-1] = 0? MonthArray [len-2]: monthArray [len-1]);}, // lunarYearMonths: function (year) {var monthData = this. lunarMonths (year); var res = []; var temp = 0; var yearData = this. lunarInfo [year-this. MIN_YEAR]; var len = (yearData [0] = 0? 12: 13); for (var I = 0; I <len; I ++) {temp = 0; for (j = 0; j <= I; j ++) {temp + = monthData [j];} res. push (temp);} return res;}, // obtain the leapMonth of the month. // @ param year: function (year) {var yearData = this. lunarInfo [year-this. MIN_YEAR]; return yearData [0];}, // calculates the number of days between the lunar date and January 1, lunar January 1. betweenLunarDays: function (year, month, day) {var yearMonth = this. lunarMonths (year); var res = 0; for (var I = 1; I <month; I ++) {res + = yearMonth [I-1];} res + = day-1; return res ;}, // calculate the number of days between two solar calendar dates // @ param year solar year // @ param month // @ param day // @ param l_month the calendar month corresponding to lunar January //@ param l_day: function (year, month, day, l_month, l_day) {var time1 = new Date (year + "-" + month + "-" + day ). getTime (), time2 = new Date (year + "-" + l_month + "-" + l_day ). getTime (); return Math. ceil (time1 -Time2)/24/3600/1000);}, // calculate the lunar calendar date based on the number of days from January 1, lunar January 1 // @ param year calendar year // @ param between number of days lunarByBetween: function (year, between) {var lunarArray = [], yearMonth = [], t = 0, e = 0, leapMonth = 0, m = ''; if (between = 0) {t = 1; e = 1; m = 'August 1';} else {year = between> 0? Year: (year-1); yearMonth = this. lunarYearMonths (year); leapMonth = this. leapMonth (year); between = between> 0? Between: (this. lunarYearDays (year) + between); for (var I = 0; I <13; I ++) {if (between = yearMonth [I]) {t = I + 2; e = 1; break;} else if (between <yearMonth [I]) {t = I + 1; e = between-(yearMonth [I-1])? YearMonth [I-1]: 0) + 1; break;} m = (leapMonth! = 0 & t = leapMonth + 1 )? ('Salesman'. this. chineseMonth (t-1): this. chineseMonth (leapMonth! = 0 & leapMonth + 1 <t )? (T-1): t);} lunarArray. push (year, t, e); // year month day lunarArray. push (this. lunarYear (year), this. zodiacYear (year), m, this. chineseNumber (e); // supports the lunarArray of the year, month, and day of the Chinese zodiac. push (leapMonth); // returns lunarArray for months;}, // chineseMonth for Chinese months: function (month) {var monthHash = ['', 'february 6 ', 'octoken ', 'August 1']; return monthHash [month] ;}, // Chinese Date chineseNumber: function (num) {var dateHash = ['', '1', '2 ', '3', '4', '5', '6', '7', '8', '9', '10']; if (num <= 10) {res = 'start' + dateHash [num];} else if (num> 10 & num <20) {res = '10' + dateHash [num-10];} else if (num = 20) {res = "20";} else if (num> 20 & num <30) {res = "res" + dateHash [num-20];} else if (num = 30) {res = "Thirty" ;}return res ;}, // convert the lunar toLunar: function (year, month, day) {var yearData = this. lunarInfo [year-this. MIN_YEAR]; if (year = this. MIN_YEAR & month <= 2 & day <= 9) {return [1891, 1, 1, 'sinmao ', 'rabbit', 'August 1 ', '1'];} return this. lunarByBetween (year, this. betweenSolarDays (year, month, day, yearData [1], yearData [2]);}, // convert the Gregorian calendar // @ param year lunar calendar-year // @ param month Lunar Calendar-month, leap month processing: for example, if the current year expires on January 1, the second month will be transferred to January 1, it is equivalent to a lunar calendar with 13 months. // @ param date: Lunar Calendar-day toSolar: function (year, month, day) {var yearData = this. lunarInfo [year-this. MIN_YEAR]; var between = this. betweenLunarDays (year, month, day); var MS = new Date (year + "-" + yearData [1] + "-" + yearData [2]). getTime (); var s = MS + between * 24*60*60*1000; var d = new Date (); d. setTime (s); year = d. getFullYear (); month = d. getMonth () + 1; day = d. getDate (); return [year, month, day] ;}};
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.