以下是js / 初始化日期选择 var now = new Date(); var nowYear = now.getFullYear(); var nowMonth = now.getMonth() + 1; var nowDay = now.getDate();
// 数据初始化 function formatYear() { var arr = []; /* 事故日期显示100年内 */ for (var i = nowYear - 99; i <= nowYear; i++) { arr.push({ id: i + '', value: i + '' }); } return arr; }
function formatMonth(month) { var arr = []; for (var i = 1; i <= month; i++) { arr.push({ id: i + '', value: i < 10 ? ('0' + i) : (i + '') }); } return arr; }
function formatDate(day) { var arr = []; for (var i = 1; i <= day; i++) { arr.push({ id: i + '', value: i < 10 ? ('0' + i) : (i + '') }); } return arr; }
var yearData = function (callback) { callback(formatYear()); } var monthData = function (year, callback) { if (nowYear == year) { callback(formatMonth(nowMonth)); } else { callback(formatMonth(12)); } }; var dateData = function (year, month, callback) { if (nowYear == year && nowMonth == month) {// 如果是今年的本月,那么只能选择今天以前的日期 callback(formatDate(nowDay)); } else if (/^(1|3|5|7|8|10|12)$/.test(month)) { callback(formatDate(31)); } else if (/^(4|6|9|11)$/.test(month)) { callback(formatDate(30)); } else if (/^2$/.test(month)) { if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) { callback(formatDate(29)); } else { callback(formatDate(28)); } } else { throw new Error('month is illegal'); } }; $('#three_date').bind('click', function () { /* 默认是今天 */ var _dateArr = [nowYear + '', nowMonth + '', nowDay + '']; if (_this.beneficiaryInfo.Birthday) { _dateArr = _this.beneficiaryInfo.Birthday.split('-'); } var iosSelect = new IosSelect(3, [yearData, monthData, dateData], { title: '出生日期', itemHeight: 35, oneLevelId: _dateArr[0] ? Number(_dateArr[0]) : '', twoLevelId: _dateArr[1] ? Number(_dateArr[1]) : '', threeLevelId: _dateArr[2] ? Number(_dateArr[2]) : '', showLoading: true, callback: function (selectOneObj, selectTwoObj, selectThreeObj) { var year = (selectOneObj.value).split('年')[0]; var month = (selectTwoObj.value).split('月')[0]; var day = (selectThreeObj.value).split('日')[0]; var date = year + '-' + month + '-' + day; if (!_this.beneficiaryInfo.Birthday || _this.beneficiaryInfo.Birthday != date) { // 计算有效期至 _this.computeIDExpDate(date); } _this.refreshStyle(); } }); });
picker ? 工具里?
/ 初始化日期选择
var now = new Date();
var nowYear = now.getFullYear();
var nowMonth = now.getMonth() + 1;
var nowDay = now.getDate();
// 数据初始化
function formatYear() {
var arr = [];
/* 事故日期显示100年内 */
for (var i = nowYear - 99; i <= nowYear; i++) {
arr.push({
id: i + '',
value: i + ''
});
}
return arr;
}
function formatMonth(month) {
var arr = [];
for (var i = 1; i <= month; i++) {
arr.push({
id: i + '',
value: i < 10 ? ('0' + i) : (i + '')
});
}
return arr;
}
function formatDate(day) {
var arr = [];
for (var i = 1; i <= day; i++) {
arr.push({
id: i + '',
value: i < 10 ? ('0' + i) : (i + '')
});
}
return arr;
}
var yearData = function (callback) {
callback(formatYear());
}
var monthData = function (year, callback) {
if (nowYear == year) {
callback(formatMonth(nowMonth));
} else {
callback(formatMonth(12));
}
};
var dateData = function (year, month, callback) {
if (nowYear == year && nowMonth == month) {// 如果是今年的本月,那么只能选择今天以前的日期
callback(formatDate(nowDay));
} else if (/^(1|3|5|7|8|10|12)$/.test(month)) {
callback(formatDate(31));
} else if (/^(4|6|9|11)$/.test(month)) {
callback(formatDate(30));
} else if (/^2$/.test(month)) {
if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
callback(formatDate(29));
} else {
callback(formatDate(28));
}
} else {
throw new Error('month is illegal');
}
};
$('#three_date').bind('click', function () {
/* 默认是今天 */
var _dateArr = [nowYear + '', nowMonth + '', nowDay + ''];
if (_this.beneficiaryInfo.Birthday) {
_dateArr = _this.beneficiaryInfo.Birthday.split('-');
}
var iosSelect = new IosSelect(3,
[yearData, monthData, dateData],
{
title: '出生日期',
itemHeight: 35,
oneLevelId: _dateArr[0] ? Number(_dateArr[0]) : '',
twoLevelId: _dateArr[1] ? Number(_dateArr[1]) : '',
threeLevelId: _dateArr[2] ? Number(_dateArr[2]) : '',
showLoading: true,
callback: function (selectOneObj, selectTwoObj, selectThreeObj) {
var year = (selectOneObj.value).split('年')[0];
var month = (selectTwoObj.value).split('月')[0];
var day = (selectThreeObj.value).split('日')[0];
var date = year + '-' + month + '-' + day;
if (!_this.beneficiaryInfo.Birthday || _this.beneficiaryInfo.Birthday != date) {
// 计算有效期至
_this.computeIDExpDate(date);
}
_this.refreshStyle();
}
});
});
划不动一点都划不动