码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 之日起对象(转)

时间:2018-03-13 18:12:11      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:转换   ...   方法   utc   oda   时间转换   javascrip   minutes   zone   

学习要点:

  日期对象

  将日期对象转换为字符串

  将日期对象中的日期和时间转换为字符串

  日期对象中的日期

  日期对象中的时间

  设置日期对象中的日期

  设置日期对象中的时间

  与毫秒相关的方法

一、日期对象

  在javascript中并没有日期型的数据类型,但是提供了一个日期对象可以操作日期和时间。

  日期对象的创建:

  new Date();

二、将日期对象转换为字符串

  将日期对象转换为字符串可以使用以下4种方法:

  date.toString();//将日期对象转换为字符串时,采用的是本地时间

  date.toLocalString();//将日期对象转换为字符串,采用的是本地时间,显示的是地方日期的格式

  date.toUTCString();//将日期对象转换为字符串时,采用的是世界时间。

  date.toGMTString();//将日期对象转换为字符串时,采用的是GMT时间,但是已被禁止使用,一般用toUTCString()方法来替换。

三、将日期对象中的日期和时间转换为字符串

  date.toDateString();//将日期部分转换为字符串,本地时间

  date.toLocalDateString();//将日期部分转换为字符串,采用的是本地时间,显示的是地方日期的格式

  date.toTimeString();//将时间部分转换为字符串,本地时间

  date.toLocalTimeString();将时间部分转换为字符串,采用的是本地时间,显示的是地方日期的格式

四、日期对象中的日期

  date.getYear();//获取年份,但不建议使用。

  date.getFullYear();//获取年份,,以四位数显式,建议使用       

  date.getMonth();//获取月份,值为0-11,一月份为0,二月份为1...

  date.getDate();//获取天数,即一个月中的某一天

  date.getDay();//获取一周中的第几天,值为0-6,周日为0...

五、日期对象中的时间

  date.getHours();//返回小时部分

  date.getMinutes();//返回分钟部分

  date.getSeconds();//返回秒钟部分    

  date.getMilliseconds();//返回毫秒部分

  date.getTime();//返回日期对象中的时间与1970年1月1日0时0分0秒所间隔的毫秒数

  date.getTimezoneoffset();//返回日期对象中的时间与UTC之间的时差数,单位为秒。

六、设置日期对象中的日期

  date.setYear(year);//不建议使用

  date.setFullYear(year,month,day);//year四位数;month:0-11,该参数可省略;day:1-31, 该参数可省略  

  date.setMonth(month,day);//month:0-11;day:1-31, 该参数可省略 

  date.getDate(day);//day:1-31

七、设置日期对象中的时间

  date.getHours(hours,minutes,seconds,milliseconds);//hours:0-23,minutes:0-59,可省略,seconds:0-59,可省略milliseconds:0-999,可省略

  date.getMinutes(minutes,seconds,milliseconds);//minutes:0-59,seconds:0-59,可省略milliseconds:0-999,可省略

  date.getSeconds(seconds,milliseconds);// seconds:0-59,milliseconds:0-999,可省略 

  date.getMilliseconds(milliseconds);//,milliseconds:0-999

八、与毫秒相关的方法

  date.setTime(millisecinds);milliseconds代表设置的时间与1970年1月1日0时0分0秒所间隔的毫秒数

  date.valueOf();返回日期对象中的时间与1970年1月1日0时0分0秒所间隔的毫秒数

  date.parse(str);返回str参数所代表的时间与1970年1月1日0时0分0秒所间隔的毫秒数

  date.UTC(year,month,day,hours,minutes,seconds,milliseconds);将参数所代表的日期转换成与1970年1月1日0时0分0秒所间隔的毫秒数

转自 http://www.cnblogs.com/LiuJL/p/5563606.html

JavaScript 之日起对象(转)

标签:转换   ...   方法   utc   oda   时间转换   javascrip   minutes   zone   

原文地址:https://www.cnblogs.com/potato-lee/p/8559224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!