談談javascript中的日期Date對象

1、日期對象
  在javascript中並無日期型的數據類型,可是提供了一個日期對象能夠操做日期和時間。
  日期對象的建立:
  new Date();
2、將日期對象轉換爲字符串
  將日期對象轉換爲字符串能夠使用如下4種方法:
  date.toString();//將日期對象轉換爲字符串時,採用的是本地時間
  date.toLocalString();//將日期對象轉換爲字符串,採用的是本地時間,顯示的是地方日期的格式
  date.toUTCString();//將日期對象轉換爲字符串時,採用的是世界時間。
  date.toGMTString();//將日期對象轉換爲字符串時,採用的是GMT時間,可是已被禁止使用,通常用toUTCString()方法來替換。
3、將日期對象中的日期和時間轉換爲字符串
  date.toDateString();//將日期部分轉換爲字符串,本地時間
  date.toLocalDateString();//將日期部分轉換爲字符串,採用的是本地時間,顯示的是地方日期的格式
  date.toTimeString();//將時間部分轉換爲字符串,本地時間
  date.toLocalTimeString();將時間部分轉換爲字符串,採用的是本地時間,顯示的是地方日期的格式
4、日期對象中的日期
  date.getYear();//獲取年份,但不建議使用。
  date.getFullYear();//獲取年份,,以四位數顯式,建議使用       
  date.getMonth();//獲取月份,值爲0-11,一月份爲0,二月份爲1...
  date.getDate();//獲取天數,即一個月中的某一天
  date.getDay();//獲取一週中的第幾天,值爲0-6,週日爲0...
5、日期對象中的時間
  date.getHours();//返回小時部分
  date.getMinutes();//返回分鐘部分
  date.getSeconds();//返回秒鐘部分    
  date.getMilliseconds();//返回毫秒部分
  date.getTime();//返回日期對象中的時間與1970年1月1日0時0分0秒所間隔的毫秒數
  date.getTimezoneoffset();//返回日期對象中的時間與UTC之間的時差數,單位爲秒。
6、設置日期對象中的日期
  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
7、設置日期對象中的時間
  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
8、與毫秒相關的方法
  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秒所間隔的毫秒數javascript

 

摘自:http://www.itxm.net/a/bianchengyuyan/2016/0613/318.htmlhtml

參考:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.htmljava

相關文章
相關標籤/搜索