筋JavaScript(9)-Date類

5919 ワード

常用時間:UTC(世界標準時間)、GMT(グリニッジ時間)、現地時間.
相関関数一覧
//    
Date

//Date       
Date.parse
Date.UTC

//Date        
new Date()
new Date(   )
new Date(         )
new Date( ,  ,  ,  ,  ,  ,   )

//Date        
getFullYear      (getUTCFullYear)
getMonth         (getUTCMonth)
getDate          (getUTCDate)
getDay           (getUTCDay)
getHours         (getUTCHours)
getMinutes       (getUTCMinutes)
getSeconds       (getUTCSeconds)
getMilliseconds  (getUTCMilliseconds)
getTime
getTimezoneOffset

setFullYear      (setUTCFullYear)
setMonth         (setUTCMonth)
setDate          (setUTCDate)
setHours         (setUTCHours)
setMinutes       (setUTCMinutes)
setSeconds       (setUTCSeconds)
setMilliseconds  (setUTCMilliseconds)
setTime

toDateString
toTimeString
toUTCString
toLocaleString
toLocaleDateString
toLocaleTimeString
toString
valueOf

 
 
 
 
 

 

 
  
一つ以上の数値に基づいて時間オブジェクトを作成し、ローカル時間と世界標準時間の違いを確認します.
//                  
var d = new Date(2009, 2, 27, 12, 59, 59, 999);

alert(d);                      //Fri Mar 27 12:59:59 UTC+0800 2009  
alert(d.toString());           //Fri Mar 27 12:59:59 UTC+0800 2009
alert(d.toUTCString());        //Fri, 27 Mar 2009 04:59:59 UTC
alert(d.toLocaleString());     //2009 3 27  12:59:59

alert(d.toDateString());       //Fri Mar 27 2009 
alert(d.toLocaleDateString()); //2009 3 27  

alert(d.toTimeString());       //12:59:59 UTC+0800 
alert(d.toLocaleTimeString()); //12:59:59

/*               ,     UTC     1970-1-1 0:0:0          */
alert(d.valueOf());            //1238129999999
alert(d.getTime());            //1238129999999

/*                  */
alert(d.getTimezoneOffset()); //-480;      ,     8   

/*        (   ,      : 1238129999999)       */
var d = new Date(1238129999999);
alert(d.toLocaleString());     //2009 3 27  12:59:59 

/*        2-7     ,      " ,  ,  ,  ,  ,  ,   "      */
d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d.toLocaleString());     //2009 3 27  12:59:59 

d = new Date(2009, 2, 27, 12, 59, 59);
alert(d.toLocaleString());     //2009 3 27  12:59:59 

d = new Date(2009, 2, 27, 12, 59);
alert(d.toLocaleString());     //2009 3 27  12:59:00   

d = new Date(2009, 2, 27, 12);
alert(d.toLocaleString());     //2009 3 27  12:00:00  

d = new Date(2009, 2, 27);
alert(d.toLocaleString());     //2009 3 27  0:00:00 

d = new Date(2009, 2);
alert(d.toLocaleString());     //2009 3 1  0:00:00 

/* Date        UTC             2-7  ,   UTC       number */
var n = Date.UTC(2009, 0); //                
alert(typeof n);           //number

var d = new Date(n);       //        、         
alert(d.toUTCString());    //Thu, 1 Jan 2009 00:00:00 UTC 
alert(d.toLocaleString()); //2009 1 1  8:00:00 

 
 
 
 
 

 

 
  
パラメータなしで作成した時間オブジェクトと、グローバル関数Dateで取得した時間の違い
var d1 = new Date(); //      
var d2 = Date();     //       

alert(d1);           //Fri Feb 27 13:20:58 UTC+0800 2009
alert(d2);           //Fri Feb 27 13:20:58 2009 

alert(d1.valueOf()); //1235712058340
alert(d2.valueOf()); //Fri Feb 27 13:20:58 2009 

alert(typeof d1);    //object
alert(typeof d2);    //string

//     d2      ,       String     ,       Date     .

 
 
 
 
 

 

 
  
文字列パラメータを使って時間オブジェクトを作成します.
var d;
d = new Date('Fri Mar 27 12:59:59 UTC+0800 2009');
alert(d.toLocaleString());     //2009 3 27  12:59:59

d = new Date('Fri Mar 27 12:59:59 2009');
alert(d.toLocaleString());     //2009 3 27  12:59:59

d = new Date('Fri Mar 27 2009');
alert(d.toLocaleString());     //2009 3 27  0:00:00

d = new Date('Mar 27 2009');
alert(d.toLocaleString());     //2009 3 27  0:00:00

/*     Date()        */
var ts = Date();
d = new Date(ts);
alert(d.toLocaleString());     //2009 3 27  14:04:38

/* Date        parse            ,           (     ) */
var n;
n = Date.parse('Mar 27 2009');
alert(n);                     //1238083200000 
alert(typeof n);              //number

d = new Date(n);
alert(d.toLocaleString());    //2009 3 27  0:00:00

 
 
 
 
 

 

 
  
それぞれ取得と設定:年、月、日、時、分、秒、ミリ秒のうち、「曜日」は取得できますが、設定はできません.
var d = new Date(2009, 2, 27, 12, 58, 59, 999);
alert(d.toLocaleString());  //2009 3 27  0:00:00
alert(d.getFullYear());     //2009
alert(d.getMonth());        //2 (  0  , 2   3  )
alert(d.getDate());         //27
alert(d.getDay());          //5 (   )
alert(d.getHours());        //12
alert(d.getMinutes());      //58
alert(d.getSeconds());      //59
alert(d.getMilliseconds()); //999 (  )

d.setFullYear(2010);
d.setMonth(1);
d.setDate(2);
d.setHours(3);
d.setMinutes(4);
d.setSeconds(5);
d.setMilliseconds(6);

alert(d.toLocaleString());  //2010 2 2  3:04:05
alert(d.getFullYear());     //2010
alert(d.getMonth());        //1 (  0  , 1   2  )
alert(d.getDate());         //2
alert(d.getDay());          //2 (   )
alert(d.getHours());        //3
alert(d.getMinutes());      //4
alert(d.getSeconds());      //5
alert(d.getMilliseconds()); //6 (  )

/*      setTime */
var d = new Date();
d.setTime(0);
alert(d.toUTCString()); //Thu, 1 Jan 1970 00:00:00 UTC