年积日转年月日

// 年积日转年月日
function isLeapYear(year) {
  var ret = false;
  year = parseInt(year)
  if (((0 == (year % 4)) && (0 != (year % 100))) || (0 == (year % 400))) {
    ret = true;
  } else {
    ret = false;
  }
  return ret;
}

function day2date(daytime) {
  var monthDay_common = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
  var monthDay_leap = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335];
  var monthDay;
  var month = 0;
  var day = 0;

  var year = daytime.substr(0, 4);
  var time_DD = daytime.substr(4, 3);
  var hour = daytime.substr(7, 2);
  var minute = daytime.substr(9, 2);
  
  var leapYear = isLeapYear(year)
  if (leapYear) {
    monthDay = monthDay_leap
  } else {
    monthDay = monthDay_common
  }

  for (month = 0; month < 12; month++) {
    if (monthDay[month] >= time_DD) {
      break;
    }
  }

  day = time_DD - monthDay[month - 1];
  var date = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return date;
}

day2date('201928501002505')

 

你可能感兴趣的:(JavaScript,js,年积日,年月日,转换)