【发布时间】:2016-06-13 08:59:26
【问题描述】:
我有 2 个日期选择器,开始时间和结束时间。
我希望开始时间比“现在”晚 7 小时
我有这个:
$('#<%= txtErrorStartDate.ClientID%>').val(formatDateTime(sqlNow('HH', -7), 'dateshorttime')).validate();
还有这个:
function formatDateTime(d, format, rtnObj) {
var arr
, type
, arrDate
, arrTime
, str
;
if (!d) {
if (rtnObj) {
return null;
} else {
return '';
}
}
switch (format) {
case 'shortdate':
type = 1;
break;
case 'shorttime':
type = 2;
break;
case 'dateshorttime':
type = 3;
break;
case 'datelongtime':
type = 4;
break;
case 'longtime':
type = 5;
break;
default:
if (rtnObj) {
return null;
} else {
return '';
}
}
if (typeof(d) === 'string') {
if (d.indexOf('/Date(') !== -1) {
// JSON date (milliseconds)
d = new Date(Number(d.replace('/Date(', '').replace(')/', '')));
} else {
str = true;
}
} else if (typeof(d) === 'number') {
d = new Date(d);
}
if (str) {
// Format string
d = $.trim(d);
if (d.indexOf(' ') !== -1) {
// Split date and time
arr = d.split(' ');
arrDate = arr[0].split('/');
arrTime = arr[1].split(':');
if (arrTime.length === 2) {
// Add missing seconds
arrTime.push('00');
}
} else if (d.indexOf('/') !== -1) {
// Split date
arrDate = d.split('/');
arrTime = ['00', '00', '00'];
} else {
arrDate = ['30', '12', '1899'];
arrTime = d.split(':');
if (arrTime.length === 2) {
// Add missing seconds
arrTime.push('00');
}
}
} else {
// Format Javascript date object
// Build date array
arrDate = [];
arrDate.push(d.getDate());
arrDate.push(d.getMonth() + 1);
arrDate.push(d.getFullYear());
// Build time array
arrTime = [];
arrTime.push(d.getHours());
arrTime.push(d.getMinutes());
arrTime.push(d.getSeconds());
// Single digit check
if (Number(arrDate[0]) < 10) { arrDate[0] = '0' + arrDate[0]; }
if (Number(arrDate[1]) < 10) { arrDate[1] = '0' + arrDate[1]; }
if (Number(arrTime[0]) < 10) { arrTime[0] = '0' + arrTime[0]; }
if (Number(arrTime[1]) < 10) { arrTime[1] = '0' + arrTime[1]; }
if (Number(arrTime[2]) < 10) { arrTime[2] = '0' + arrTime[2]; }
}
if (rtnObj) {
// Return Javascript object
// Take 1 from month (0 - 11)
arrDate[1] = String(Number(arrDate[1]) - 1);
switch (type) {
case 1: // shortdate
return new Date(arrDate[2], arrDate[1], arrDate[0]);
break;
case 2: // shorttime
return new Date('1899', '11', '30', arrTime[0], arrTime[1]);
break;
case 3: // dateshorttime
return new Date(arrDate[2], arrDate[1], arrDate[0], arrTime[0], arrTime[1]);
break;
case 4: // datelongtime
return new Date(arrDate[2], arrDate[1], arrDate[0], arrTime[0], arrTime[1], arrTime[2]);
break;
case 5: // longtime
return new Date('1899', '11', '30', arrTime[0], arrTime[1], arrTime[2]);
break;
}
} else {
// Return date string
switch (type) {
case 1: // shortdate
return arrDate.join('/');
break;
case 2: // shorttime
arrTime.pop();
return arrTime.join(':');
break;
case 3: // dateshorttime
arrTime.pop();
return arrDate.join('/') + ' ' + arrTime.join(':');
break;
case 4: // datelongtime
return arrDate.join('/') + ' ' + arrTime.join(':');
break;
case 5: // longtime
return arrTime.join(':');
break;
}
}
}
我有什么不工作,我想知道如何从开始时间减去 7 小时
添加日期选择器 html:
<div class="inputrow" runat="server">
<label style="margin-left: 179px" class="inputlabel">Start Date</label>
<input runat="server" type="text" id="txtErrorStartDate" name="txtErrorStartDate1" class="dateshorttime datepick required" value="" data-taborder="1" required="" style="width: 18%" />
</div>
【问题讨论】:
-
你提到了
datepicker,但我在你的代码中没有看到它! -
为 datepick 添加了 html
-
请注意,typeof 是运算符,而不是函数,因此请使用
typeof d。此外,“类型”1、3 和 4 可以在一种情况下组合,2 和 5 可以组合成另一种情况。不需要那么多开关块。 -
改变现在看起来更光滑了,干杯
标签: javascript date datepicker