JS processing JSON date format problem _javascript skills

Source: Internet
Author: User

Cause

For the date field returned from C #, when the JSON serialization, in the foreground JS display is not a real date, which makes us feel very uncomfortable, we can not for this thing, all date fields into string bar, so, find a JS extension method to achieve this function

Realize

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));
  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 ()
    + "year"
    + month
    + "month"
    + currentdate
    + "Day"
    + ""
    + Date.gethours ()
    + ":"
    + date.getminutes ();
}
Call: Changedateformat (data[i].arrdate)

Call

$.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: "Details of the present Application"});//expected to be equal to the time of the application (5th or 20th)
      }
    );

PS: Returns the JSON time such as/date (1290371638000)/form, how to deal with the YYYY-MM-DD such format

Remove/date

Direct format of 1290371638000

/**
* The format of the time object;
*
/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.lengt H==1? O[k]: ("+ o[k]"). substr (("+ o[k]). length);
  }
 return format;
}

How to use:

 var testdate = new Date ();
var teststr = Testdate.format ("yyyy mm month DD Day hh hour mm minute ss seconds");
alert (TESTSTR);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.