1、Date对象介绍
Date对象用于处理日期和时间。
创建Date对象的语法:
var myDate = new Date();
注释:Date对象会自动把当前日期和时间保存为其初始值。
2、Date对象常用方法介绍
Date():返回当日的日期和时间。
getDate():从Date对象返回一个月中的某一天(1~31)。
getDay():从Date对象返回一周中的某一天(0~6)。
getMonth():从Date对象返回月份(0~11)。
getFullYear():从Date对象以四位数字返回年份。
getHours():返回Date对象的小时(0~23)。
getMinutes():返回Date对象的分钟(0~59)。
getSeconds():返回Date对象的秒数(0~59)。
getMillseconds():返回Date对象的毫秒(0~999)。
getTime():返回1970年1月1日至今的毫秒数。
3、应用实例
应用场景:把页面中填写申请日期一个输入框改为显示当前日期,而且只读,不能够修改。
js实现代码:
<script type="text/javascript">
//页面加载时执行函数
window.onload=function(){
//写入申请日期
fixApplyTime();
}
//固定申请日期
function fixApplyTime(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth()+1);
if(month<10){
month = "0" + month;
}
var day = date.getDate();
var currentTime = year + "-" + month + "-" + day;
document.getElementById("applyTime").value = currentTime;
}
</script>
标签实现代码:
<td class="tdContentEven-alignRight" width="23%">
<font class="blue02">*申请日期</font>
</td>
<td class="tdContentOdd-alignLeft" width="27%">
<input type="text" readonly="readonly" name="applyTime" id="applyTime" class="timeWdate" style="width:100px" />
</td>