How to format JSON date?

2019-05-04 09:25发布

so, i need format JSON date from this format

"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed

i need do this using jQuery

also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/

so anyone have idea?

4条回答
对你真心纯属浪费
2楼-- · 2019-05-04 09:33

Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here: http://www.mattkruse.com/javascript/date/

Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:

data = JSON.parse(data, function (key, value) {
    // parsing MS serialized DateTime strings
    if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
        return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
        // maybe new Date(parseInt(value.substr(6))) also works and it's simpler
    }
    return value;
});
查看更多
Fickle 薄情
3楼-- · 2019-05-04 09:44

you can create a Date Object from a string like so:

var myDate = new Date(dateString);

then you can manipulate it anyway you want, one way to get your desired output is:

var output = myDate.getDate() + "\\" +  (myDate.getMonth()+1) + "\\" + myDate.getFullYear();

you can find more at this elated.com article "working with dates"

查看更多
Bombasti
4楼-- · 2019-05-04 09:44

Try something like this :

var date = new Date(parseInt(jsonDate.substr(6))); 

where jsonDate is variable that stores your date

查看更多
虎瘦雄心在
5楼-- · 2019-05-04 09:48

The code below solved my problem:

var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
查看更多
登录 后发表回答