How do I convert a decimal year value into a Date in Javascript?

前端 未结 3 1241
无人及你
无人及你 2020-12-21 05:43

OK, this is basically a Javascript version of How can I convert a decimal year value into a Date in Ruby? and not exactly a duplicate of Javascript function to conv

相关标签:
3条回答
  • 2020-12-21 06:01

    The other solution would be:

    1. Create date for given year (integer part)
    2. Calculate days from reminder (decimal part) and convert to milliseconds
    3. Add milliseconds to (1)

    In script:

    function leapYear(year) {
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    };
    
    function convertDecimalDate(decimalDate) {
        var year = parseInt(decimalDate);
        var reminder = decimalDate - year;
        var daysPerYear = leapYear(year) ? 366 : 365;
        var miliseconds = reminder * daysPerYear * 24 * 60 * 60 * 1000;
        var yearDate = new Date(year, 0, 1);
        return new Date(yearDate.getTime() + miliseconds);
    }
    
    var date = convertDecimalDate(2015.0596924);
    console.log(date);

    You can play with it on this Fiddle.

    0 讨论(0)
  • 2020-12-21 06:11

    JavaScript will resolve the dates for you if you add too much time. See demonstration below. The solution below doesn't calculate the leap year based on the algorithm, but takes next year's date and subtracts it from this year. This assumes that the JavaScript specification properly calculates leap years.

    See Mozilla Docs for more info.

    function decimalDateToJsDate(time) {
      var year = Math.floor(time);
      var thisYear = new Date(year, 0, 1);
      var nextYear = new Date(year + 1, 0, 1);
      var millisecondsInYear = nextYear.getTime() - thisYear.getTime();
      var deltaTime = Math.ceil((time - year) * millisecondsInYear);
      thisYear.setMilliseconds(deltaTime);
      return thisYear;
    }
    
    document.getElementById("output").innerHTML = decimalDateToJsDate(2015.0596924);
    <pre id="output"></pre>

    0 讨论(0)
  • 2020-12-21 06:17
    function leapYear (year){
      return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }
    
    function getMonthAndDayFromDayOfYear(dayOfYear, year){
      var daysInMonthArray = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
      if (leapYear(year)) { daysInMonthArray[2] = 29; }
    
      var daysLeft = dayOfYear;
      var month = 0;
      for (i=0; i<daysInMonthArray.length; i++) {
        var daysInThisMonth = daysInMonthArray[i];
        if (daysLeft > daysInThisMonth) {
          month += 1;
          daysLeft -= daysInThisMonth;
        } else {
          break;
        }
      }
      return { month: month, day: daysLeft };
    }
    
    function convertDecimalDate(decimalDate){
      decimalDate = parseFloat(decimalDate);
      var year = parseInt(decimalDate); // Get just the integer part for the year
      var daysPerYear = leapYear(year) ? 366 : 365; // Set days per year based on leap year or not
      var decimalYear = decimalDate - year; // A decimal representing portion of the year left
      var dayOfYear = Math.ceil(decimalYear * daysPerYear); // day of Year: 1 to 355 (or 366)
      var md = getMonthAndDayFromDayOfYear(dayOfYear, year);
      var day = md['day'];
      var month = md['month'];
      return new Date(year,month,day);
    }
    
    var date = convertDecimalDate(2015.0596924);
    
    0 讨论(0)
提交回复
热议问题