前面有好幾篇文章是說明在後端的程式中要如何去對物件資料序列化為JSON,
而物件資料序列化為JSON之後就是要傳給前端去使用,然而JSON資料卻會有個小小的問題存在,
JSON規格中沒有對日期時間的型別去做定義,在 .NET 環境下的日期時間資料在序列化之後卻會有個特別的表示方法,
讓使用者知道某個欄位的資料是個日期型別,
如果是直接已JsonResult的Json方法做資料的序列化,則日期型別的資料會是以下格式:
\/Date(1316747376403)\/
如果是用Json.NET的JsonConvert.SerializeObject()來序列化,則日期型別的資料會是以下格式:
\/Date(1316747376403+0800)
\/
除非是在後端的程式中就已經對日期型別的資料去做處理,不然傳到前端的日期資料都還是要再做個處理。
為何會有 \/Date(Tick)\/ 的格式出現,請參閱以下的文章:
MSDN - An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET
而黑暗執行緒的部落格也有一篇文章是說明JSON的日期轉換,開頭的地方就有說明JSON的日期型別的呈現方式與格式:
黑暗大的文章裡面也有詳細的說明要如何將後端序列化的JSON日期資料去轉換為我們所熟悉的日期格式。
ASP.NET的JSON日期轉換
接下來就來實地操練一下,
測試範例資料:
後端產生JSON資料的程式:
這邊是從Entity Framework把物件資料給取出來,然後使用Select()指定要輸出的欄位資料,
以 JsonResult Json()方法 將選定的資料給序列化為JSON.
public JsonResult GetNodesJson()
{var collection = service.GetCollection().OrderBy(x => x.CreateDate).Select(x => new
{ID = x.ID,Name = x.Name,CreateDate = x.CreateDate,UpdateDate = x.UpdateDate});return Json(collection, JsonRequestBehavior.AllowGet);
}
序列化後的JSON資料:
使用Fiddler2查看JSON內容:
轉換JSON的日期資料
方法一:使用正則式(Regular Expression)
eval(日期資料.replace(/\/Date\((.*?)\)\//gi, "new Date($1)"))
function LoadJsonDataAndConvert()
{$.ajax({url: '<%= Url.Action("GetNodesJson", "Test") %>',type: 'post',dataType: 'json',async: false,
cache: false,
success: function (data)
{if (data)
{var content = "Use Regular Expression";$.each(data, function (i, item)
{content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): " + eval(item.CreateDate.replace(/\/Date\((.*?)\)\//gi, "new Date($1)"));content += "<br/>";
});$('#JsonContent').html(content);}}});}
執行結果:
方法二:使用substr(6) and parseInt
new Date(parseInt(日期資料.substr(6)), 10)
function LoadJsonDataAndConvert()
{$.ajax({url: '<%= Url.Action("GetNodesJson", "Test") %>',type: 'post',dataType: 'json',async: false,
cache: false,
success: function (data)
{if (data)
{var content = "Use Regular Expression";$.each(data, function (i, item)
{content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): " + new Date(parseInt(item.CreateDate.substr(6), 10));content += "<br/>";
});$('#JsonContent').html(content);}}});}
執行結果:
方法三:使用Json4ms.js
Json4ms.js是微軟MVP Rick Strahl 所以建立的一組JavaScript函式,我們可以將函式內容給另存為Json4ms.js,
然後要記得在網頁中要去引用「json4ms.js」.
if (JSON && !JSON.parseWithDate) {
var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/;
JSON.parseWithDate = function(json) {
/// <summary>
/// parses a JSON string and turns ISO or MSAJAX date strings
/// into native JS date objects
/// </summary>
/// <param name="json" type="var">json with dates to parse</param>
/// </param>
/// <returns type="value, array or object" />
try {
var res = JSON.parse(json,
function(key, value) {
if (typeof value === 'string') {
var a = reISO.exec(value);
if (a)
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
a = reMsAjax.exec(value);
if (a) {
var b = a[1].split(/[-+,.]/);
return new Date(b[0] ? +b[0] : 0 - +b[1]);
}
}
return value;
});
return res;
} catch (e) {
// orignal error thrown has no error message so rethrow with message
throw new Error("JSON content could not be parsed");
return null;
}
};
JSON.stringifyWcf = function(json) {
return JSON.stringify(json, function(key, value) {
if (typeof value == "string") {
var a = reISO.exec(value);
if (a) {
var val = '/Date(' + new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])).getTime() + ')/';
this[key] = val;
return val;
}
}
return value;
})
};
JSON.dateStringToDate = function(dtString) {
/// <summary>
/// Converts a JSON ISO or MSAJAX string into a date object
/// </summary>
/// <param name="" type="var">Date String</param>
/// <returns type="date or null if invalid" />
var a = reISO.exec(dtString);
if (a)
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
a = reMsAjax.exec(dtString);
if (a) {
var b = a[1].split(/[-,.]/);
return new Date(+b[0]);
}
return null;
};
}
而這一組函式中,我們會使用到的是以下兩個function:
依據黑暗大的文章「ASP.NET的JSON日期轉換」說明,
JSON.stringifyWcf(), 將物件轉成JSON字串時將Date物件轉成 "\/Date(Tick)\/" 格式.
JSON.parseWithDate(), 將 "\/Date(Tick)\/" 的字串解析為 JavaScript的Date物件.
所以我們程式修改如下:
function LoadJsonDataAndConvert()
{$.ajax({url: '<%= Url.Action("GetNodesJson", "Test") %>',type: 'post',dataType: 'json',async: false,
cache: false,
success: function (data)
{if (data)
{var content = "Use Json4ms.js";$.each(data, function (i, item)
{content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): " + JSON.parseWithDate(JSON.stringifyWcf(item.CreateDate));
content += "<br/>";
});$('#JsonContent').html(content);}}});}
執行結果:
格式化JavaScript日期物件的字串輸出
上面的三種JSON日期資料的轉換結果,以第一筆資料來看,
原本是「\/Date(1316747376403)\/」的字串經過轉換後就顯示為「Fri Sep 23 2011 11:09:36 GMT+0800」,
這個結果好像與資料庫中的日期資料「2011-09-23 11:09:36.403」有所不同,
因為上面三種方法都是將JSON日期資料給轉為JavaScript的日期物件,所以直接輸出的字串就是我們看到的結果,
如果需要將JavaScript的日期物件輸出為我們想要的格式,如「yyyy - MM - dd HH : mm : ss」,
可以使用現有的套件來當幫助我們做日期資料格式化的工作。
"f" is for Format & WHAT THE diff??
在 demo 的部落格中(demoshop)有篇文章介紹到一個JavaScript的套件「f」,
「f」是個蠻方變得JS套件,這個套件的工作就是「"f" is for Format & WHAT THE diff??」
"f" is for Format
Date.f Method
- WHAT THE diff??
- Date.diff Method
- And Friends...
Date.getDaysInMonth Method | Date.isLeapYear Method | Date.isDayLightSavingsDay Method | Date.getDayLightSavingsDays Method
不過我們這邊就只會用到 「f()」
我們就拿方法三的程式來做修改,加入 f() 的日期格式化功能:
function LoadJsonDataAndConvert()
{$.ajax({url: '<%= Url.Action("GetNodesJson", "Test") %>',type: 'post',dataType: 'json',async: false,
cache: false,
success: function (data)
{if (data)
{var content = "Use Json4ms.js";$.each(data, function (i, item)
{content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): " + JSON.parseWithDate(JSON.stringifyWcf(item.CreateDate)).f("yyyy-MM-dd HH:mm:ss");content += "<br/>";
});$('#JsonContent').html(content);}}});}
輸出結果:
CreateDate: “\/Date(1316747376403)\/” 轉換並格式化為 CreateDate(Formatted): 2011-09-23 11:09:36
另外有關日期格式化的部份,網路上有多種的套件,接下來介紹一下除了「f」之外的兩個套件。
jQuery plugin – jquery dateFormat
http://plugins.jquery.com/project/jquery-dateFormat
https://github.com/phstc/jquery-dateFormat
http://pablocantero.com/blog/2010/09/04/jquery-plugin-javascript-for-java-util-date-tostring-format/
使用的方式相當簡單,首先你先下載JS檔案:https://github.com/phstc/jquery-dateFormat/blob/master/jquery.dateFormat-1.0.js
並在網頁中去引用這個JS檔案,
而JS程式中的使用方式為:$.format.date(日期物件或是日期字串, "yyyy-MM-dd HH:mm:ss");
拿前面的程式來做修改:
function LoadJsonDataAndConvert()
{
$.ajax({
url: '<%= Url.Action("GetNodesJson", "Test") %>',
type: 'post',
dataType: 'json',
async: false,
cache: false,
success: function (data)
{
if (data)
{
var content = "Use jquery-dateFormat<br/><br/>";
$.each(data, function (i, item)
{
content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): ";
content += $.format.date(new Date(parseInt(item.CreateDate.substr(6), 10)), "yyyy-MM-dd HH:mm:ss");
content += "<br/>";
});
$('#JsonContent').html(content);
}
}
});
}
執行結果:
日期的格式化樣式是依據「java.text.SimpleDateFormat」http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Date and time patterns
yyyy = year
MM = month
MMM = month abbreviation (Jan, Feb … Dec)
MMMM = long month (January, February … December)
ddd = day of the week in words (Monday, Tuesday … Sunday)
dd = day
hh = hour in am/pm (1-12)
HH = hour in day (0-23)
mm = minute
ss = second
a = am/pm marker
SSS = milliseconds
基本上日期格式化的樣式都是差不多的,但就是有的函式就是會比較不一樣,
接下就介紹這個使用不一樣日期格式化的JavaScript函式。
Flagrant Badassery - JavaScript Date Format
http://blog.stevenlevithan.com/archives/date-time-format
http://stevenlevithan.com/assets/misc/date.format.js
函式原始碼:
/*
* Date Format 1.2.3
* (c) 2007-2009 Steven Levithan <stevenlevithan.com>
* MIT license
*
* Includes enhancements by Scott Trenda <scott.trenda.net>
* and Kris Kowal <cixar.com/~kris.kowal/>
*
* Accepts a date, a mask, or a date and a mask.
* Returns a formatted version of the given date.
* The date defaults to the current date/time.
* The mask defaults to dateFormat.masks.default.
*/
var dateFormat = function ()
{
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
timezoneClip = /[^-+\dA-Z]/g,
pad = function (val, len)
{
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};
// Regexes and supporting functions are cached through closure
return function (date, mask, utc)
{
var dF = dateFormat;
// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date))
{
mask = date;
date = undefined;
}
// Passing date through Date applies Date.parse, if necessary
date = date ? new Date(date) : new Date;
if (isNaN(date)) throw SyntaxError("invalid date");
mask = String(dF.masks[mask] || mask || dF.masks["default"]);
// Allow setting the utc argument via the mask
if (mask.slice(0, 4) == "UTC:")
{
mask = mask.slice(4);
utc = true;
}
var _ = utc ? "getUTC" : "get",
d = date[_ + "Date"](),
D = date[_ + "Day"](),
m = date[_ + "Month"](),
y = date[_ + "FullYear"](),
H = date[_ + "Hours"](),
M = date[_ + "Minutes"](),
s = date[_ + "Seconds"](),
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
flags = {
d: d,
dd: pad(d),
ddd: dF.i18n.dayNames[D],
dddd: dF.i18n.dayNames[D + 7],
m: m + 1,
mm: pad(m + 1),
mmm: dF.i18n.monthNames[m],
mmmm: dF.i18n.monthNames[m + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12),
H: H,
HH: pad(H),
M: M,
MM: pad(M),
s: s,
ss: pad(s),
l: pad(L, 3),
L: pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return mask.replace(token, function ($0)
{
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
} ();
// Some common format strings
dateFormat.masks = {
"default": "ddd mmm dd yyyy HH:MM:ss",
shortDate: "m/d/yy",
mediumDate: "mmm d, yyyy",
longDate: "mmmm d, yyyy",
fullDate: "dddd, mmmm d, yyyy",
shortTime: "h:MM TT",
mediumTime: "h:MM:ss TT",
longTime: "h:MM:ss TT Z",
isoDate: "yyyy-mm-dd",
isoTime: "HH:MM:ss",
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};
// Internationalization strings
dateFormat.i18n = {
dayNames: [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
],
monthNames: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
};
// For convenience...
Date.prototype.format = function (mask, utc)
{
return dateFormat(this, mask, utc);
};
使用的方式如下:
(new Date(parseInt(日期資料.substr(6)))).format(formatString)
其中 formatString 為日期格式化的樣式,雖然說與其他格式化的函式使用都大致相同,
但是其中就是有兩個地方介不一樣,「M」「m」…
一般來說,「M」都表示為月份 month,「m」則表示為分鐘 minute,
但是這個 JavaScript Date Format 就是把這兩個給相反使用,「M」用來表示分鐘,「m」用來表示月份,
所以當要顯示日期格式為「年-月-日 時:分:秒」,那個格式化樣式就要用「yyyy-mm-dd HH:MM : ss」
(new Date(parseInt(日期資料.substr(6)))).format("yyyy-mm-dd HH:MM:ss")
程式內容:
function LoadJsonDataAndConvert()
{
$.ajax({
url: '<%= Url.Action("GetNodesJson", "Test") %>',
type: 'post',
dataType: 'json',
async: false,
cache: false,
success: function (data)
{
if (data)
{
var content = "Use JavaScripot Date Format<br/><br/>";
$.each(data, function (i, item)
{
content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): ";
content += (new Date(parseInt(item.CreateDate.substr(6), 10))).format("yyyy-mm-dd HH:MM:ss");
content += "<br/>";
});
$('#JsonContent').html(content);
}
}
});
}
執行結果:
如果你真的很不能夠適應的話,其實我們也可以對原始碼去做個修改,將月份與分鐘的格式化符號調換回來,
這樣一來就可以繼續的使用我們已經使用習慣的日期格式化樣式。
修改的JavaScript Date Format:
/*
* Date Format 1.2.3
* (c) 2007-2009 Steven Levithan <stevenlevithan.com>
* MIT license
*
* Includes enhancements by Scott Trenda <scott.trenda.net>
* and Kris Kowal <cixar.com/~kris.kowal/>
*
* Accepts a date, a mask, or a date and a mask.
* Returns a formatted version of the given date.
* The date defaults to the current date/time.
* The mask defaults to dateFormat.masks.default.
*
* Modified by mrkt: Change Month use M, and Change Minute use m
* 2011-10-31
*/
var dateFormat = function ()
{
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
timezoneClip = /[^-+\dA-Z]/g,
pad = function (val, len)
{
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};
// Regexes and supporting functions are cached through closure
return function (date, mask, utc)
{
var dF = dateFormat;
// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date))
{
mask = date;
date = undefined;
}
// Passing date through Date applies Date.parse, if necessary
date = date ? new Date(date) : new Date;
if (isNaN(date)) throw SyntaxError("invalid date");
mask = String(dF.masks[mask] || mask || dF.masks["default"]);
// Allow setting the utc argument via the mask
if (mask.slice(0, 4) == "UTC:")
{
mask = mask.slice(4);
utc = true;
}
var _ = utc ? "getUTC" : "get",
d = date[_ + "Date"](),
D = date[_ + "Day"](),
M = date[_ + "Month"](),
y = date[_ + "FullYear"](),
H = date[_ + "Hours"](),
m = date[_ + "Minutes"](),
s = date[_ + "Seconds"](),
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
flags = {
d: d,
dd: pad(d),
ddd: dF.i18n.dayNames[D],
dddd: dF.i18n.dayNames[D + 7],
M: M + 1,
MM: pad(M + 1),
MMM: dF.i18n.monthNames[M],
MMMM: dF.i18n.monthNames[M + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12),
H: H,
HH: pad(H),
m: m,
mm: pad(m),
s: s,
ss: pad(s),
l: pad(L, 3),
L: pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return mask.replace(token, function ($0)
{
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
} ();
// Some common format strings
dateFormat.masks = {
"default": "ddd MMM dd yyyy HH:mm:ss",
shortDate: "M/d/yy",
mediumDate: "MMM d, yyyy",
longDate: "MMMM d, yyyy",
fullDate: "dddd, MMMM d, yyyy",
shortTime: "h:mm TT",
mediumTime: "h:mm:ss TT",
longTime: "h:mm:ss TT Z",
isoDate: "yyyy-MM-dd",
isoTime: "HH:mm:ss",
isoDateTime: "yyyy-MM-dd'T'HH:mm:ss",
isoUtcDateTime: "UTC:yyyy-MM-dd'T'HH:mm:ss'Z'",
customDateTime: "yyyy-MM-dd HH:mm:ss"
};
// Internationalization strings
dateFormat.i18n = {
dayNames: [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
],
monthNames: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
};
// For convenience...
Date.prototype.format = function (mask, utc)
{
return dateFormat(this, mask, utc);
};
而改使用修改過函式的程式內容:
function LoadJsonDataAndConvert()
{
$.ajax({
url: '<%= Url.Action("GetNodesJson", "Test") %>',
type: 'post',
dataType: 'json',
async: false,
cache: false,
success: function (data)
{
if (data)
{
var content = "Use JavaScripot Date Format (modified)<br/><br/>";
$.each(data, function (i, item)
{
content += "Name: " + item.Name;
content += " || Sort: " + item.Sort;
content += " || CreateDate: " + item.CreateDate;
content += " || CreateDate(Formatted): ";
content += (new Date(parseInt(item.CreateDate.substr(6), 10))).format("yyyy-MM-dd HH:mm:ss");
content += "<br/>";
});
$('#JsonContent').html(content);
}
}
});
}
執行結果:
好了,講得長長一大篇,最後整理一下:
將後端程式所序列化的JSON日期資料給轉換為JavaScript日期物件
方法一:使用正則式(Regular Expression)
方法二:使用substr(6) and parseInt
方法三:使用Json4ms.js
轉換為日期物件後,使用套件輸出格式化的日期字串
套件一:"f" is for Format & WHAT THE diff??
套件二:jQuery plugin – jquery dateFormat
套件三:Flagrant Badassery - JavaScript Date Format
延伸閱讀:
MSDN - An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET
Tales from the Evil Empire - Dates and JSON
ASP.NET AJAX - How do I Serialize Dates with JSON?
參考連結:
demoshop - 簡單解決.NET 序列化為 JSON 後日期時間的問題
以上
沒有留言:
張貼留言