js Date对象的使用

来源:互联网 发布:路灯照明计算软件 编辑:程序博客网 时间:2024/05/08 12:40


一、Date对象的创建


1、var objDate=new Date();   //Date 对象自动使用当前的日期和时间作为其初始值。


2、var objDate=new Date(milliseconds);    //将给定的毫秒数转换为使用的时间


参数:


milliseconds {int} :毫秒数;表示从'1970/01/01 00:00:00'为起点,开始叠加的毫秒数。



3、new Date(dateStr)   //把字符串转换为Date对象


参数:


dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:


1) yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。


2) yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。



4、var objDate=new Date((yearmonthdate[hours[minutes[seconds[,ms]]]]));  //指定具体的日期


参数:


①year {int} :年份;4位数字。如:1999、2014


②month {int} :月份;2位数字。从0开始计算,0表示1月份、11表示12月份。


③opt_day {int} 可选:号; 2位数字;从1开始计算,1表示1号。


④opt_hours {int} 可选:时;2位数字;取值0~23。


⑤opt_minutes {int} 可选:分;2位数字;取值0~59。


⑥opt_seconds {int} 可选:秒;2未数字;取值0~59。


⑦opt_milliseconds {int} 可选:毫秒;取值0~999。



二、get方法


方法描述getFullYear()返回四位数的年份(如:2013)getYear()根据浏览器的不同返回两位或者四位数的年份getMonth()返回用整数表示的月份,从0(1月)到11(12月)getDate()返回日期,从1开始getDay()返回星期几,从0(星期日)到6(星期六)getHours()返回小时数,从0到23(24小时制)getMinutes()返回分钟数,从0到59getSeconds()返回秒数,从0到59getMilliSeconds()返回毫秒数,从0到999getTime()返回从GMT时间1970年1月1日0点0分0秒经过的毫秒数




三、set方法


方法描述setFullYear(year, opt_month, opt_date)设置Date对象的年份值;4位年份setMonth(month, opt_date)设置Date对象的月份值。0表示1月,11表示12月setDate(date)设置Date对象的月份中的日期值;值的范围1~31setHours(hour, opt_min, opt_sec, opt_msec)设置Date对象的小时值setMinutes(min, opt_sec, opt_msec)设置Date对象的分钟值setSeconds(sec, opt_msec)设置Date对象的秒数值setMilliseconds(msec)设置Date对象的毫秒值




文章转载自 : js Date对象     http://www.studyofnet.com/news/831.html


0 0
原创粉丝点击