谈谈javascript中的日期Date对象

一、日期对象
  在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.itxm.net/a/bianchengyuyan/2016/0613/318.html

参考:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

原文地址:https://www.cnblogs.com/csniper/p/6210763.html