您的位置:首页 > Web前端 > JavaScript

【JAVAScript】js为日期框设置默认时间

2016-05-10 20:13 651 查看
使用js为日期控件设置,默认的时间。这里演示的是,为日期控件设置当前系统前七天。



<script type="text/javascript">
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;
}
</script>

设置format,可以规范日期格式,因为java.util.Date时英文的显示,所以先设置format,规范显示的格式。

当前系统前七天:
<script type="text/javascript">
$(function(){
var t=new Date();
t.setTime(t.getTime()-7*24*3600*1000);
document.getElementById("StartDate").value = t.format("yyyy-MM-dd");
});
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: