首页 > 代码库 > Javascript中的Date
Javascript中的Date
Date类用来处理日期和时间,基于1970年1月1日(世界标准时间)起的毫秒数(时间戳)
1、创建日期对象
var now = new Date();//当前时间 var date = new Date(1000);//1970年1月1日起过了秒钟的时间 var date = new Date(year,month,day,hour,minute,second,millisecond);//通过分别指定各个时间分量来创建日期对象
2、提供的方法
getTime()获取一个Date对象所基于的时间戳
此外还提供了一系列的getter/setter方法来操作各个时间分量,如getHours()
注意:除了getMonth()(获取月份)的返回值是从0开始,其余都是从1开始
示例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Javascript测试</title> <script type="text/javascript"> var now = new Date(); alert(now); alert("时间戳: " + now.getTime()); var year = now.getFullYear(); var month = now.getMonth(); var day = now.getDate(); alert("年月日:" + year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day)); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); alert("时分秒:" + hour + ":" + (minute<10?"0"+minute:minute) + ":" + (second<10?"0"+second:second)); </script> </head> <body> </body> </html>
效果图
Javascript中的Date
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。