本文实例讲述了JavaScript日期对象(Date)基本用法。分享给大家供大家参考,具体如下:
1.获取当前日期:
document.write("Current time: "+new Date());2.获取时间戳(毫秒):
document.write(new Date().getTime());3.设置年月日(年为必选,月日为可选):
var d = new Date();d.setFullYear(2016,3,16)document.write(d);document.write("<br/>");document.write(d.getTime());4.返回当前星期几:
document.write(new Date().getDay())5.日期对象的比较:
function compareDate(){ var oneday = new Date(); oneday.setFullYear(2016,2,26); var currentday = new Date(); if (oneday<currentday){ alert("before"); }else{ alert("after"); }}效果图:
示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://pareDate(){ var oneday = new Date(); oneday.setFullYear(2016,2,26); var currentday = new Date(); if (oneday<currentday){ alert("before"); }else{ alert("after"); } } </script></body></html>PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线秒表工具:
http://tools.jb51.net/bianmin/miaobiao
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
在线世界各地时间查询:
http://tools.jb51.net/zhuanhuanqi/worldtime
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。