首頁  >  文章  >  web前端  >  javascript中Date物件的使用總結

javascript中Date物件的使用總結

高洛峰
高洛峰原創
2016-12-06 10:20:331135瀏覽

JSON 日期轉JS日期,我們知道,日期類型轉成JSON之後,回傳的資料類似這樣:

  /Date(1379944571737)/

  但是這種日期並不能直接顯示這是沒有人知道這一點意思,下面提供一個JSON日期轉JS日期的方式。

function ConvertJSONDateToJSDate(jsondate) {
 var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
 return date;
}

   


在提供兩種Date轉習慣視覺的日期格式:

//yyyy-MM-dd
function getDate(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 return year + "-" + month + "-" + day ;
}
//yyyy-MM-dd HH:mm:SS
function getDateTime(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 var hh = date.getHours();
 var mm = date.getMinutes();
 var ss = date.getSeconds();
 return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}

   


一、Date .getDate()  回傳是日期物件中月份中的幾號。

var str = "2012-12-12";
var date = new Date(str);  //字符串转换为Date对象
document.write(date.getFullYear());  //然后就可以使用Date对象的方法输出年份了

   


二、Date.getDay()  返回日期中的星期幾  星期日0-星期6

var date = new Date();  //2012-12-19
document.write(date.getDate());  //返回 19 是19号

 返回年份  如2012。

var date = new Date();
document.write(date.getDay());  //3 星期3

   

四、Date.getHours()  返回日期中的小時,幾點了,0-23

var date = new Date();
document.write(date.getFullYear());  //返回2012,2012年

.日期中的毫秒數

var date = new Date();
document.write(date.getHours());  //返回23,晚上11点

   


六、Date.getMinutes()    返回日期中的分鐘數0-59

var date = new Date();
document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒
🎀   //回傳日期中的月份數,回傳值0 (1月)-11(12月)

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分

   


八、Date.getSeconds()    //返回一個日期的描述

      //將一個日期物件以毫秒形式回傳

var date = new Date();
document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

   


十、Date.getTimezoneOffset()   //GMT時間與本地時間差,用分鐘錶示

UTCDate()    / /傳回Date物件中的日期值,(全球時間)

var date = new Date();
document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒

   


十二、Date.getUTCDay()     //返回Dater

 十三、Date.getUTCFullYear()  //返回Date中的年份,4位,如2012,(全球時間)

var date = new Date();
document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

   

小時數,就是現在是幾點,終於有一個跟getHours()不同了,應該是時差關係,回傳的是全球時間裡的。

var date = new Date();
document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。

   


十五、Date.getUTCMilliserconds()  //返回Date物件中的毫秒數,(全球時間)

var date = new Date();
document.write(date.getUTCDate());  //返回19  19号

75%

    //返回Date物件中的分鐘數,(全球時間)

var date = new Date();
document.write(date.getUTCDay());  //返回3  星期3

   

十七、Date.getUTCMonth()      ///Date對像中月份值,(全球時間)  
十八、 Date.getUTCSeconds()    //傳回Date物件中的秒數值

var date = new Date();
document.write(date.getUTCFullYear());  //返回2012

   


十九、Date.getYear() 〦〟〜 

二十、Date.now()    靜態方法//返回1970-01-01午夜到現在的時間間隔,用毫秒錶述

var date = new Date();
document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。

   

『時間字串,傳回1970-01-01午夜到給定日期之間的毫秒數

var date = new Date();
document.write(date.getMilliseconds());  //返回全球时间中的毫秒数

   


二十二、Date.setDate()  //設定一個Date

二十二、Date.setDate()  //設定一個Date中的日期值,返回物件值以調整後的日期的毫秒錶示

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。

   


 二十三、Date.setFullYear()  //設定一個Date物件中的年份,回傳毫值調整後的日期的秒數。

var date = new Date();
document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  

   


二十四、Date.setHours()  //設定一個Date物件中的小事數,傳回值以調整後的日期的毫秒錶示。

var date = new Date();
document.write(date.getSeconds());  //返回秒数值 返回33

   


二十五、Date.setMilliseconds()  //設定一個日期的毫秒數

var date = new Date();
document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)

. //設定一個日期的分鐘數

document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。

   

二十七、Date.setMonth()      //設定一個日期的月份數

var date = "2012-12-19";
document.write(Date.parse(date));  //返回  1355875200000
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //输出2012-11-19  //注意月份是从0-11

reee

, 二十三

.     //設定一個日期的描述語法:date.setSeconds(seconds)        date.setSeconds(seconds,millis)

var date = new Date();
document.write(date.setDate(11));  //返回1355236647980    //设置为11,其实是12月,设置为3其实是4月
    var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意

 //使用毫秒數設定一個時間語法:date.setTime(milliseonds)

var date = new Date();  今天是2012-12-20
document.write(date.setFullYear(1989)); //返回630167981030
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出1989-11-20

   

三十、Date.setUTCDate()        //設定一個Date對像中對應月的日期值,就是幾號(全球時間  //設定一個Date物件中對應月的日期值,就是幾號(全球時間 )date.

var date = new Date(); //现在是2012-12-52 22:52
document.write(date.setHours(5)); //返回1355954000882
var da = new Date(date);
document.write("<br/>" + da.getHours()); //输出05

   

三十一、Date.setUTCFullYear()     //设置一个Date对象中对应的年份,全球时间      语法:date.setUTCFullYear(year)         date.setUTCFullYear(year,month)         date.setUTCFullYear(year,month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCFullYear(1999)); //返回945702713666
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1999

   


三十二、Date.setUTCHours()      //设置一个Date对象中对应的小时数,(全球时间)      语法:date.setUTCHours(hours)          date.setUTCHours(hours,minutes)         date.setUTCHours(hours,minutes,seconds)         date.setUTCHours(hours,minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCHours(05)); //返回1355980581928
var da = new Date(date);
document.write("<br/>" + da.getUTCHours()); //输出5

   


三十三、Date.setUTCMilliseconds()  //设置一个Date对象中对应的毫秒数,(全球时间)     语法:date.setUTCMilliseconds(millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(05)); //返回1356016784005  注意此处无论如何刷新都是05结尾

   


三十四、Date.setUTCMinutes()    //设置一个Date对象的分钟、秒钟、以及毫秒值。     语法:date.setUTCMinutes(minutes)     date.setUTCMinutes(minutes,seconds)                date.setUTCMinutes(minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCMinutes(25)); //返回1356017146549
var da = new Date(date);
document.write("<br/>" + da.getUTCMinutes()); //输出5

   


三十五、Date.setUTCMonth()    //设置一个Date对象的月份值及日期值        语法:date.setUTCMonth(month)          date.setUTCMonth(month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(01)); //返回1329751527983
var da = new Date(date);
document.write("<br/>" + da.getUTCMonth()); //输出1

   


三十六、Date.setUTCSeconds()    //设置一个Date的秒钟及毫秒值      语法:date.setUTCSeconds(seconds)         date.setUTCSeconds(seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCSeconds(01)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getUTCSeconds()); //输出1

   


三十七、Date.setYears()      //设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数                    则把它当成FullYear设置      语法:date.setYears(year)

var date = new Date(); //现在是2012-12-20
document.write(date.setYear(22)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1922
var date = new Date(); //现在是2012-12-20
document.write(date.setYear(2011)); //返回1324395113386
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出2011

   


三十八、Date.toDateString()    //以字符串的形式返回一个Date的日期部分     语法:date.toDateString()

var date = new Date(); //现在是2012-12-20
document.write(date.toDateString("yyyy-MM-dd")); //返回Thu Dec 20 2012

   


三十九、Date.toTimeString()    //以字符串的形式返回一个Date的时间部分     语法:date.toTimeString()

var date = new Date(); //现在是2012-12-20
document.write(date.toTimeString("yyyy-MM-dd")); //返回23:38:33 GMT+0800

   


 四十、Date.toISOString()      //将一个Date对象转换为ISO-8601格式的字符串     语法;date.toISOString()  //返回的字符串格式为yyyy-mm-ddThh:mm:ssZ

var date = new Date(); //现在是2012-12-20
document.write(date.toISOString()); //返回2012-12-20T15:45:47.493Z

   


四十一、Date.toJSON      //JSON序列化一个对象     语法:date.toJSON(key)  //date的一个字符串表示形式,值为调用它的toISOString()方法的结果

var date = new Date(); //现在是2012-12-20
document.write(date.toJSON()); //返回2012-12-20T15:45:47.493Z

   


四十二、Date.toLocaleDateString()  //以本地格式的字符串返回一个Date的日期部分语法:date.toLolcaleDateString  //返回一个本地人可读的日期格式,日期部分

var date = new Date(); //现在是2012-12-20
document.write(date.toLocaleDateString()); //返回2012年12月20日

   


 四十三、Date.toLocaleString()    //将一个Date转化难为一个本地格式的字符串 语法:date.toLocaleString()

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleString()); //返回2012年12月22日 19:56:40

   


四十四、Date.toLocaleTimeString()    //将一个Date转化为本地的格式的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleTimeString()); //返回19:57:23

   


四十五、Date.toString()          //将一个Date转换为一个字符串

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十六、Date.toTimeString()       //以字符串的形式返回一个Date对象的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十七、Date.toUTCString()      //将一个Date对象转换为字符串(全球时间)

var date = new Date(); //现在是2012-12-22
document.write(date.toUTCString()); //返回Sat, 22 Dec 2012 12:00:42 GMT

   


四十八、Date.UTC()        //将一个Date对象转换毫秒的形式  静态方法语法:Date.UTC(year,month,day,hours,minutes,seconds,ms)

document.write(Date.UTC(2011, 11, 11, 11, 11, 11)); //返回1323601871000


四十九、Date.valueOf() 

//如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示

var date = "";
document.write(date.valueOf()); //不是Date对象,不输出
var date1 = new Date();
document.write(date1.valueOf()); //输出1356180400916

   



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn