javascript 如何格式化JSON日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9363263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:25:03  来源:igfitidea点击:

How to format JSON date?

javascriptjqueryjsondatetime-format

提问by hullfan

so, i need format JSON date from this format

所以,我需要这种格式的格式 JSON 日期

"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SSto 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

“9/30/2010 12:00:00 AM”,它是MM/DD/YYYY HH:MM:SS格式如下:DD/MM/YYYY,所以我不需要关于小时、分钟和秒的信息,以及我需要从 json 中替换几个月和几天,我尝试了一些不同的方法,但总是失败

i need do this using jQuery

我需要使用jQuery做到这一点

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

我也没有找到格式化这个日期类型的任何答案,我发现的只是格式化这样的日期:/Date(1224043200000)/

so anyone have idea?

所以有人知道吗?

采纳答案by JKirchartz

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"

您可以在这篇elated.com 文章“处理日期”中找到更多信息

回答by peterfoldi

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/

不幸的是,您的“来自”日期格式不是 JavaScript 中与实现无关的日期格式。并且所有其他格式都取决于实现,这意味着即使大多数实现都可以理解这种格式,我/您也不能确定例如如何解析 DD 和 MM 顺序(我几乎可以肯定它会取决于当地的区域设置)。因此,我建议使用第 3 方(或您的手写)日期解析器从您的输入字符串中获取 Date 对象。您可以在这里找到一个这样的解析器: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:

因为您的问题对我来说不是 100% 清楚,所以您的日期可能采用 /Date(number)/ 格式,这表明您正在从您的 jQuery 代码调用 ASP.Net 服务。在这种情况下,您可以在 JSON 解析期间将其转换为 Date 对象:

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;
});

回答by Vinod kumar

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();

回答by Dana Addler

Try something like this :

尝试这样的事情:

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

where jsonDate is variable that stores your date

其中 jsonDate 是存储日期的变量