Development log: generate date data in json format

In the project, because of the limitation of the plug-in mPicker.js used, I wrote a piece of code to generate the date json, and record it here.

The generated format is as follows, there are 12 months in the year child, and there are corresponding days under the child:
json format

The code can generate dates for the first 49 years, the next 50 years, and the current year, a total of 100 years:

function getDateJson() {
    
    
	var _prevY = [],
    	_nextY = [],
    	_allY = [],
    	_allM = [],
    	_allD = [];
	var def = new Date(),
    	year = def.getFullYear(),
        month = def.getMonth() + 1,
        day = def.getDate();
	for (var i = 1; i <= 31; i++) {
    
     // 日
		_allD.push({
    
     "name": (i < 10 ? ('0' + i) : i) + '日', "value": (i < 10 ? ('0' + i) : i) })
	}
	for (var i = 1; i <= 12; i++) {
    
     // 月
		var _monthD = Object.assign([], _allD);
		switch(i) {
    
    
    		case 4, 6, 9, 11:
	    		_monthD.splice(_monthD.length, 1);
    			break;
    		default:
    			break;
		}
		_allM.push({
    
     "name": (i < 10 ? ('0' + i) : i) + '月', "value": (i < 10 ? ('0' + i) : i), child: _monthD });
	}
	for (var i = 49; i >= 0; i--) {
    
    
		var _upY = year + i + 1;
		var _downY = year - i;
		var _yearD = Object.assign([], _allM);

		// 后50年
		if((_downY%4==0 && _downY%100!=0) || (_downY%100==0 && _downY%400==0)) {
    
    
			_yearD[1].child.splice(28, 1)
			_prevY.push({
    
     "name": _downY + '年', "value": _downY + '', child: _yearD });
		} else {
    
    
			_yearD[1].child.splice(29, 1)
			_prevY.push({
    
     "name": _downY + '年', "value": _downY + '', child: _yearD });
		}

		// 前50年
		if((_upY%4==0 && _upY%100!=0) || (_upY%100==0 && _upY%400==0)) {
    
    
			_yearD[1].child.splice(28, 1)
			_nextY.unshift({
    
     "name": _upY + '年', "value": _upY + '', child: _yearD });
		} else {
    
    
			_yearD[1].child.splice(29, 1)
			_nextY.unshift({
    
     "name": _upY + '年', "value": _upY + '', child: _yearD });
		}
	}
	_allY = _prevY.concat(_nextY);
	return _allY;
}

Attached:

Calculation of the number of days in
  February    The number of days in February mainly depends on whether the year is a leap year, 29 days if it is a leap year, and 28 days if it is not a leap year.
  Generally, in mathematical operations, or in the Gregorian calendar year method, most of the years that can be divisible by 4 are leap years, except for those years that can be divisible by 100 but not by 400.
  1. A non-century year can be divisible by 4, and a leap year is not divisible by 100. (For example, 2004 is a leap year, 1901 is not a leap year)
  2. The leap year that can be divisible by 400 in the century is a leap year. (Such as 2000 is a leap year, 1900 is not a leap year)

Guess you like

Origin blog.csdn.net/qq_37992222/article/details/112324707