通用方法:
//格式化日期类型 自定义函数来格式化时间
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
自定义函数来格式化时间
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
给id为beginDate的input赋值当前时间,格式为yyyy-mm-dd
$("#beginDate").val(new Date().format('yyyy-MM-dd'));
$('#endDate').val(new Date().format('yyyy-MM-dd'));
即使input中已经定义了,但感觉没有什么作用.
<label for="beginDate" class="col-md-4 control-label">开始日期</label>
<div class="col-md-8">
<input name="beginDate" id="beginDate" cssClass="form-control"
style="height: 34px;border-radius: 4px;"
onclick="WdatePicker({maxDate:'#F{$dp.$D(\'endDate\')}',dateFmt:'yyyy-MM-dd',readOnly:true,alwaysUseStartDate:true});"
placeholder="请选择下单日期"
value='<fmt:formatDate value="<%=new Date()%>" pattern="yyyy-MM-dd "/>'/>
</div>
<label for="endDate" class="col-md-4 control-label">结束日期</label>
<div class="col-md-8">
<input name="endDate" id="endDate" cssClass="form-control"
style="height: 34px;border-radius: 4px;"
onclick="WdatePicker({minDate:'#F{$dp.$D(\'beginDate\')}',maxDate:'%y-%M-%d',readOnly:true,dateFmt:'yyyy-MM-dd',alwaysUseStartDate:true});"
placeholder="请选择下单日期"
value='<fmt:formatDate value="<%=new Date()%>" pattern="yyyy-MM-dd "/>'/>
</div>
js中常用的几种类型有(其实我感觉这几种类型一点儿也不常用):
代码如下:
var d = new Date();
console.log(d); // 输出:Mon Nov 04 2013 21:50:33 GMT+0800 (中国标准时间)
console.log(d.toDateString()); // 日期字符串,输出:Mon Nov 04 2013
console.log(d.toGMTString()); // 格林威治时间,输出:Mon, 04 Nov 2013 14:03:05 GMT
console.log(d.toISOString()); // 国际标准组织(ISO)格式,输出:2013-11-04T14:03:05.420Z
console.log(d.toJSON()); // 输出:2013-11-04T14:03:05.420Z
console.log(d.toLocaleDateString()); // 转换为本地日期格式,视环境而定,输出:2013年11月4日
console.log(d.toLocaleString()); // 转换为本地日期和时间格式,视环境而定,输出:2013年11月4日 下午10:03:05
console.log(d.toLocaleTimeString()); // 转换为本地时间格式,视环境而定,输出:下午10:03:05
console.log(d.toString()); // 转换为字符串,输出:Mon Nov 04 2013 22:03:05 GMT+0800 (中国标准时间)
console.log(d.toTimeString()); // 转换为时间字符串,输出:22:03:05 GMT+0800 (中国标准时间)
console.log(d.toUTCString()); // 转换为世界时间,输出:Mon, 04 Nov 2013 14:03:05 GMT
另外,刚好写到这里,Wdatapicker中使用时间控件的方式:
maxDate:'#F{$dp.$D(\'endDate\')}' //最大日期不能超过结束日期
minDate:'#F{$dp.$D(\'beginDate\')}' //最小日期不能小于开始日期
maxDate:'%y-%M-%d' //最大日期是当天时间
尤其是在控制开始时间和结束时间的逻辑中非常好用.两段代码.简单有效.开始日期不能小于结束日期,不能大于当前时间.结束日期不能大于开始日期.