摘自: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")); }