欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS處理json日期格式化問題

 更新時(shí)間:2015年10月01日 08:38:39   投稿:mrr  
這篇文章主要介紹了JS處理json日期格式化問題的相關(guān)資料,需要的朋友可以參考下

起因

對(duì)于從C#返回的日期字段,當(dāng)進(jìn)行JSON序列化后,在前臺(tái)JS里顯示的并不是真正的日期,這讓我們感覺很不爽,我們不可能為了這東西,把所有日期字段都變成string吧,所以,找了一個(gè)JS的擴(kuò)展方法,來實(shí)現(xiàn)這個(gè)功能

實(shí)現(xiàn)

function ChangeDateFormat(jsondate) {
  jsondate = jsondate.replace("/Date(", "").replace(")/", "");
  if (jsondate.indexOf("+") > 0) {
    jsondate = jsondate.substring(0, jsondate.indexOf("+"));
  }
  else if (jsondate.indexOf("-") > 0) {
    jsondate = jsondate.substring(0, jsondate.indexOf("-"));
  }

  var date = new Date(parseInt(jsondate, 10));
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

  return date.getFullYear()
    + "年"
    + month
    + "月"
    + currentDate
    + "日"
    + " "
    + date.getHours()
    + ":"
    + date.getMinutes();
}
//調(diào)用:ChangeDateFormat(data[i].arrDate)

調(diào)用

$.ajax({
      type: "Get",
      textType: "json",
      url: "/UserInfo/GetUserWithdraw",
      data: { id: id },
      success: function (data) {
        var result = html.replace(reg, function (node, key) {
          return {
            'Money': data.Money,
            'AddTime': ChangeDateFormat(data.AddTime),
            'CashTime': data.CashTime
          }[key];
        });

        TsingdaTips.ask({ msg: result, show_btn: false, title: "提現(xiàn)申請(qǐng)?jiān)斍? });//預(yù)計(jì)打款時(shí)間等于申請(qǐng)時(shí)音后的(5號(hào)或20號(hào))
      }
    });

PS:返回的json時(shí)間如 /Date(1290371638000)/ 形式,怎樣處理成 yyyy-MM-dd 這類格式

去掉/Date

直接格式化1290371638000

/**
* 時(shí)間對(duì)象的格式化;
*/
Date.prototype.format = function(format){
 /*
 * eg:format="YYYY-MM-dd hh:mm:ss";
 */
 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;
}

使用方法:

 var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小時(shí)mm分ss秒");
alert(testStr);

相關(guān)文章

最新評(píng)論