摘自:http://www.cnblogs.com/CodingArt/archive/2012/06/19/2554707.html
使用jQuery.Ajax向WCF傳遞日期時,需將Date類型,或者字符串日期,轉換爲wcf需要的格式。
找到了一個辦法,見:http://www.cnblogs.com/haogj/archive/2011/12/15/2289393.html
// 爲 jQuery 擴展一個解析 wcf 日期的方法
jQuery.extend(
{
wcfDate2JsDate: function (wcfDate) {
var date = new Date(parseInt(wcfDate.substring(6)));
return date;
},
jsDate2WcfDate: function (jsDate) {
return "\/Date(" + jsDate.getTime() + "+0000)\/";
}
}
);
使用:
$.jsDate2WcfDate(new Date('1970/1/1 00:00:00'));
jQuery.Ajax從WCF取得日期,格式是:/Date(‘…’)/,需要將其轉換爲javascript的Date對象。
使用:
$.wcfDate2JsDate('/Date('...')/');
/**
* 格式化日期
* 格式 yyyy-MM-dd hh:mm:ss
*/
Date.prototype.format = function(format) {
var o =
{
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) {
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
return format;
}
/**
* 加上天數返回新的日期
* iDay 天數
*/
Date.prototype.addDay = function(iDay) {
var d = new Date(this);
d.setDate(d.getDate() + iDay);
return d;
}
/**
* 加上月數返回新的日期
* iMonth 月數
*/
Date.prototype.addMonth = function(iMonth) {
var d = new Date(this);
d.setMonth(d.getMonth() + iMonth);
return d;
}
/**
* 獲得周的第一天日期
*/
Date.prototype.getWeekFirstDate = function() {
//中國周的第一天是週一
//return this.addDay(-this.getDay() + 1);
//周的第一天是週日
return this.addDay(-this.getDay());
}
/**
* 獲得周的最後一天日期
*/
Date.prototype.getWeekLastDate = function() {
//中國周的最後一天是週日
//return this.addDay(7 - this.getDay());
//周的最後一天是週六
return this.addDay(6 - this.getDay());
}
/**
* 獲得月的第一天日期
*/
Date.prototype.getMonthFirstDate = function() {
return new Date(this.getYear(), this.getMonth(), 1);
}
/**
* 獲得月的最後一天日期
*/
Date.prototype.getMonthLastDate = function() {
return new Date(this.getYear(), this.getMonth() + 1, 0);
}
/**
* 獲得星期幾
*/
Date.prototype.getWeekStr = function() {
var weekNumber = ["日", "一", "二", "三", "四", "五", "六"];
return "星期" + weekNumber[this.getDay()];
}
function test() {
alert("今天加一天是:" + new Date().addDay(1).format("yyyy-MM-dd hh:mm:ss"));
alert("今天加一個月是:" + new Date().addMonth(1).format("yyyy-MM-dd hh:mm:ss"));
alert("今天是星期幾:" + new Date().getWeekStr());
alert("當週第一天:" + new Date().getWeekFirstDate().format("yyyy-MM-dd"));
alert("當週最後一天:" + new Date().getWeekLastDate().format("yyyy-MM-dd"));
alert("當月第一天:" + new Date().getMonthFirstDate().format("yyyy-MM-dd"));
alert("當月最後一天:" + new Date().getMonthLastDate().format("yyyy-MM-dd"));
}