A simple example of the conversion between JavaScript and Gregorian calendar _javascript tips

Source: Internet
Author: User

The

looks like this:

/** usage * lunar.tosolar (2016, 6, 3); Lunar Transformation Gregorian Calendar * Lunar.tolunar (2016, 7, 6); Gregorian translation of the lunar calendar/var lunar = {min_year:1891, max_year:2100, Lunarinfo: [[0,2,9, 21936], [6,1,30, 9656], [0,2,17 , 9584], [0,2,6, 21168], [5,1,26,43344], [0,2,13,59728], [0,2,2, 27296], [3,1,22,44368], [0,2,10,43856], [8,1,30,19304 ], [0,2,19,19168], [0,2,8, 42352], [5,1,29,21096], [0,2,16,53856], [0,2,4, 55632], [4,1,25,27304], [0,2,13,22176], [0, 2, 2, 39632], [2,1,22,19176], [0,2,10,19168], [6,1,30,42200], [0,2,18,42192], [0,2,6, 53840], [5,1,26,54568], [0, 2, 14,46400], [0,2,3, 54944], [2,1,23,38608], [0,2,11,38320], [7,2,1, 18872], [0,2,20,18800], [0,2,8, 42160], [5,1,28,456 56], [0,2,16,27216], [0,2,5, 27968], [4,1,24,44456], [0,2,13,11104], [0,2,2, 38256], [2,1,23,18808], [0,2,10,18800], [ 6,1,30,25776], [0,2,17,54432], [0,2,6, 59984], [5,1,26,27976], [0,2,14,23248], [0,2,4, 11104], [3,1,24,37744], [0,2,11 , 37600], [7,1,31,51560], [0,2,19,51536], [0,2,8, 54432], [6,1,27,55888], [0,2,15,46416], [0,2,5, 22176], [4,1,25,43736], [0,2,13, 9680], [0,2,2, 37584], [2,1,22,51544], [0,2,10,43344], [ 7,1,29,46248], [0,2,17,27808], [0,2,6, 46416], [5,1,27,21928], [0,2,14,19872], [0,2,3, 42416], [3,1,24,21176], [0,2,12 , 21168], [8,1,31,43344], [0,2,18,59728], [0,2,8, 27296], [6,1,28,44368], [0,2,15,43856], [0,2,5, 19296], [4,1,25,4  2352], [0,2,13,42352], [0,2,2, 21088], [3,1,21,59696], [0,2,9, 55632], [7,1,30,23208], [0,2,17,22176], [0,2,6, 38608], [5,1,27,19176], [0,2,15,19152], [0,2,3, 42192], [4,1,23,53864], [0,2,11,53840], [8,1,31,54568], [0,2,18,46400], [0, 2, 7, 46752], [6,1,28,38608], [0,2,16,38320], [0,2,5, 18864], [4,1,25,42168], [0,2,13,42160], [10,2,2,45656], [0,2,20,272
    16], [0,2,9, 27968], [6,1,29,44448], [0,2,17,43872], [0,2,6, 38256], [5,1,27,18808], [0,2,15,18800], [0,2,4, 25776], [3,1,23,27216], [0,2,10,59984], [8,1,31,27432], [0,2,19,23232], [0,2,7, 43872], [5,1,28,37736], [0,2,16,37600], [0, 2, 5, 51552], [4,1,24,54440], [0,2,12,54432], [0,2,1, 55888], [2,1,22,23208], [0,2,9, 22176], [7,1,29,43736], [0,2,18, 9680], [0,2,7, 37584], [  5,1,26,51544], [0,2,14,43344], [0,2,3, 46240], [4,1,23,46416], [0,2,10,44368], [9,1,31,21928], [0,2,19,19360], [0,2,8, 42416], [6,1,28,21176], [0,2,16,21168], [0,2,5, 43312], [4,1,25,29864], [0,2,12,27296], [0,2,1, 44368], [2,1,22,1  9880], [0,2,10,19296], [6,1,29,42352], [0,2,17,42208], [0,2,6, 53856], [5,1,26,59696], [0,2,13,54576], [0,2,3, 23200], [3,1,23,27472], [0,2,11,38608], [11,1,31,19176],[0,2,19,19152], [0,2,8, 42192], [6,1,28,53848], [0,2,15,53840], [0, 2, 4, 54560], [5,1,24,55968], [0,2,12,46496], [0,2,1, 22224], [2,1,22,19160], [0,2,10,18864], [7,1,30,42168], [0,2,17,421
    60], [0,2,6, 43600], [5,1,26,46376], [0,2,14,27936], [0,2,2, 44448], [3,1,23,21936], [0,2,11,37744], [8,2,1, 18808], [0,2,19,18800], [0,2,8, 25776], [6,1,28,27216], [0,2,15,59984], [0,2,4, 27424], [4,1,24,43872], [0,2,12,43744], [0, 2, 2, 37600], [3,1,21,51568], [0,2,9, 51552], [7,1,29,54440], [0,2,17,54432], [0,2,5, 55888], [5,1,26,23208], [0,2,14,22176], [0,2,3, 42704], [  4,1,23,21224], [0,2,11,21200], [8,1,31,43352], [0,2,19,43344], [0,2,7, 46240], [6,1,27,46416], [0,2,15,44368], [0,2,5, 21920], [4,1,24,42448], [0,2,12,42416], [0,2,2, 21168], [3,1,22,43320], [0,2,9, 26928], [7,1,29,29336], [0,2,17,2  7296], [0,2,6, 44368], [5,1,26,19880], [0,2,14,19296], [0,2,3, 42352], [4,1,24,21104], [0,2,10,53856], [8,1,30,59696], [0,2,18,54560], [0,2,7, 55968], [6,1,27,27472], [0,2,15,22224], [0,2,5, 19168], [4,1,25,42216], [0,2,12,42192], [0, 2, 1, 53584], [2,1,21,55592], [0,2,9, 54560]],//Whether leap years isleapyear:function (year) {return (year% 4 = 0 &&A mp Year% 100!= 0) | |
  (Year% 400 = 0)); (year) {var gan = [' Geng ', ' xin ', ' ren ', ' lunaryear:function ', ' A ', ' B ', ' C ', ' ding ', ' e ', ' self '], Zhi = [' Shen ', ' unitary ', ' Xu ', ' hai ', ' zi ', ' ugly ', ' Yin ', ' Mao ', ' Chen ', ' si ', ' afternoon ', ' not ', str = year.tostring (). Split ("");
    Return gan[str[3]] + zhi[year% 12];
    },//Zodiac year Zodiacyear:function {var zodiac = [' monkey ', ' chicken ', ' dog ', ' pig ', ' rat ', ' ox ', ' tiger ', ' Rabbit ', ' dragon ', ' snake ', ' horse ', ' sheep '];
  Return zodiac[year% 12]; },//Gregorian month days//@param year Gregorian calendar-years//@param month Gregorian calendar-month solarmonthdays:function (month) {var febdays = this . Isleapyear (year)?
    29:28;
    var monthhash = [', Febdays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  return Monthhash[month];
    },///Lunar calendar Days lunarmonthdays:function (year, month) {var monthdata = this.lunarmonths (year);
  return monthdata[month-1]; Lunarmonths:function (year) {var yeardata = This.lunarinfo[year-this for the number of days in lunar calendar.
    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;
    ///////@param year of the lunar year lunaryeardays:function {var montharray = this.lunaryearmonths);
    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; },//Get the leap month//@param year Leapmonth:function {var yeardata = this.lunarinfo[year-this.
    Min_year];
  return yeardata[0]; //calculates the number of days between the lunar calendar and the first day of the month 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;  //calculates the number of days between 2 Gregorian dates//@param year @param month//@param day//@param l_month lunar Month//@param l_day Gregorian calendar Day betweensolardays:function (year, month, days, 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); The date of the lunar Calendar//@param year @param between days according to the number of days from the first day of the month lunarbybetween:function (between) {Var lu
    Nararray = [], yearmonth = [], t = 0, E = 0, Leapmonth = 0, M = ';
      if (between = = 0) {t = 1;
      e = 1;
    m = ' first month ';
      else {year = between > 0. Year: (YEAR-1);
      Yearmonth = this.lunaryearmonths (year);
      Leapmonth = This.leapmonth (year); between = between &GT 0?
      Between: (This.lunaryeardays (year) + between);
          for (var i = 0; i < 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)? (' Leap '. This.chinesemonth (t-1)): This.chinesemonth ((leapmonth!= 0 && leapmonth + 1 < T)?
    (t-1): t)); } lunararray.push (year, T, E); Year of Lunararray.push (This.lunaryear), This.zodiacyear (year), M, This.chine Senumber (e)); Zodiac Lunararray.push (Leapmonth) year of the lunar calendar year;
  Leap a few months return lunararray; },//Chinese month chinesemonth:function (month) {var monthhash = [', ' First month ', ' February ', ' March ', ' April ', ' May ', ' June ', ' July ', ' August ', ' nine
    Months ', ' October ', ' Winter months ', ' month '];
  return Monthhash[month]; //Chinese Date
  Chinesenumber:function (num) {var datehash = [', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine ', ' ten '];
    if (num <=) {res = ' early ' + datehash[num];
    else if (num > && num <) {res = ' ten ' + datehash[num-10];
    else if (num = =) {res = ' 20 ';
    else if (num > && num <) {res = "20" + datehash[num-20];
    else if (num = =) {res = ' 30 ';
  return res; },//Convert 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, ' Xin Mao ', ' Rabbit ', ' first month ', ' first '];
  Return to This.lunarbybetween (year, This.betweensolardays, month, day, yeardata[1], yeardata[2)); },//convert Gregorian calendar//@param year Lunar calendar-years//@param month lunar calendar-months, Leap month processing: For example, if the year leap May, then the second May on the June, the equivalent of the lunar calendar has 13 months//@param date Lunar calendar-day Tosola R: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]; }
};

The above is a small series for everyone to bring the JavaScript and Gregorian calendar conversion of a simple example of all the content, I hope that we support the cloud-Habitat Community ~

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.