DIV CSS 佈局教程網

JavaScript Date 對象方法大全:從 JSON 日期到格式化輸出
編輯:關於JavaScript     

先把 JSON 日期還原成 Date

後端返回的日期轉成 JSON 之後,往往長這樣:

/Date(1379944571737)/

這串東西沒人能直接看懂,需要先還原成 Date 對象:

function ConvertJSONDateToJSDate(jsondate) {
    var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
    return 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:

var str = "2012-12-12";
var date = new Date(str);   //字符串轉換為 Date 對象
document.write(date.getFullYear());  //接著就能用 Date 的方法輸出年份

一、取值類方法(本地時間)

getDate() 返回月份中的第幾天。

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

getDay() 返回星期幾,週日為 0,週六為 6。

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

getFullYear() 返回四位年份,如 2012。

var date = new Date();
document.write(date.getFullYear());  //2012

getHours() 返回小時,範圍 0–23。

var date = new Date();
document.write(date.getHours());  //23,晚上十一點

getMilliseconds() 返回毫秒數。

getMinutes() 返回分鐘,範圍 0–59。

getSeconds() 返回秒數。

var date = new Date();          //2012-12-19 23:27:34
document.write(date.getMinutes()); //27
document.write(date.getSeconds()); //34

getMonth() 返回月份值,0 表示 1 月,11 表示 12 月,這是新手最容易踩的一點。

var date = new Date();          //2012-12-19
document.write(date.getMonth()); //11

getTime() 以毫秒形式返回該日期,數值等於 1970-01-01 午夜到當前時間的間隔。

var date = new Date();
document.write(date.getTime());  //1355930928466

getTimezoneOffset() 返回 GMT 與本地時間的差值(分鐘),實際用途是判斷運行環境的時區。

var date = new Date();
document.write(date.getTimezoneOffset());  //-480

getYear() 返回年份減去 1900 的結果,屬於遺留方法,不建議在新代碼中使用。

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

二、UTC(全球時間)取值方法

把方法名中的 Date 換成 UTC,取到的就是全球時間:

var date = new Date();
document.write(date.getUTCDate());      //19,幾號
document.write(date.getUTCDay());       //3,星期三
document.write(date.getUTCFullYear());  //2012
document.write(date.getUTCMonth());     //11,同樣 0–11
document.write(date.getUTCSeconds());   //秒數
document.write(date.getUTCMilliseconds());
document.write(date.getUTCMinutes());

小時的差值最明顯——北京時間 23:44 時,getUTCHours() 返回的是 15:

var date = new Date();  //北京時間 2012-12-19 23:44
document.write(date.getUTCHours());  //15

三、靜態方法

Date.now() 返回 1970-01-01 午夜到現在的毫秒數。

document.write(Date.now());

Date.parse() 解析日期字符串,返回 1970-01-01 午夜到該日期之間的毫秒數。

var date = "2012-12-19";
document.write(Date.parse(date));  //1355875200000
var da = new Date(date);
document.write(da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //2012-11-19

Date.UTC() 把各項時間參數轉換為毫秒數。

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

四、set 系列:修改日期的某一項

所有 set 方法都會返回調整後日期的毫秒表示。

var date = new Date();  //假設當前是 2012-12-20
document.write(date.setDate(11));        //設定為 11 號
document.write(date.setFullYear(1989));  //年份改為 1989
document.write(date.setHours(5));        //小時改為 5
document.write(date.setMilliseconds(22));
document.write(date.setMinutes(1));
document.write(date.setMonth(2));        //注意 0–11 的規則
document.write(date.setSeconds(3));
document.write(date.setTime(1356015783872));  //以毫秒數設定整個時間

對應的 UTC 版本為 setUTCDate()、setUTCFullYear()、setUTCHours()、setUTCMilliseconds()、setUTCMinutes()、setUTCMonth()、setUTCSeconds(),用法一致,只是操作的是全球時間。其中幾個支援可選參數,例如:

date.setUTCHours(hours);
date.setUTCHours(hours, minutes);
date.setUTCHours(hours, minutes, seconds);
date.setUTCHours(hours, minutes, seconds, millis);

date.setUTCMonth(month);
date.setUTCMonth(month, day);

setYear() 是個需要小心的方法:傳入 0–99 之間的值時它會自動加上 1900,當作 1900–1999 年處理;傳入四位數則等同 setFullYear()。

var date = new Date();  //2012-12-20
document.write(date.setYear(22));    //輸出 1922
var date2 = new Date(); //2012-12-20
document.write(date2.setYear(2011)); //輸出 2011

五、格式化輸出方法

var date = new Date();  //2012-12-20

document.write(date.toDateString());       //Thu Dec 20 2012
document.write(date.toTimeString());       //23:38:33 GMT+0800
document.write(date.toISOString());        //2012-12-20T15:45:47.493Z
document.write(date.toJSON());             //2012-12-20T15:45:47.493Z
document.write(date.toLocaleDateString()); //2012年12月20日
document.write(date.toLocaleTimeString()); //19:57:23
document.write(date.toLocaleString());     //2012年12月22日 19:56:40
document.write(date.toString());           //Sat Dec 22 2012 19:59:17 GMT+0800
document.write(date.toUTCString());        //Sat, 22 Dec 2012 12:00:42 GMT

其中 toISOString() 輸出固定為 yyyy-mm-ddThh:mm:ssZ 格式,toJSON() 的返回值就是呼叫 toISOString() 的結果,因此 JSON 序列化時日期會自動變成 ISO 字符串。

valueOf() 若對象是 Date,返回毫秒數;否則不輸出。

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

實務建議:月份一律記得 +1,格式化輸出統一走自己封裝的函數或成熟庫,避免各處重複拼接出錯。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved