jQuery 如何将 json 日期格式转换为以下 dd/mm/yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5248924/
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-08-26 18:48:55  来源:igfitidea点击:

How do I convert a json date format to the following dd/mm/yyyy

jqueryjsondate

提问by Calibre2010

I've seen other examples but I am looking for this specific format if possible?.

我看过其他示例,但如果可能,我正在寻找这种特定格式?

回答by nsingh

Here is a quick format from JSON date format to required date format using jQuery:

这是使用 jQuery 从 JSON 日期格式到所需日期格式的快速格式:

For a JSON date string like:

对于 JSON 日期字符串,例如:

/Date(1339439400000)/

try:

尝试:

var newFormattedDate = $.datepicker.formatDate('mm/dd/yy', new Date(Date(your_JSON_date_string)));

and it will result in date like this: 09/14/2012

它会导致这样的日期:09/14/2012

回答by Thulasiram

    function formatJsonDate(jsonDate) {
        return (new Date(parseInt(jsonDate.substr(6)))).format("dd/mm/yyyy");
    };    

    var testJsonDate = formatJsonDate('/Date(1224043200000)/');

    alert(testJsonDate);

回答by Vincent Mimoun-Prat

There is no "json date format". json only returns strings.

没有“json 日期格式”。json 只返回字符串。

Date javascript object can parse a variety of formats. See the documentation.

日期 javascript 对象可以解析多种格式。请参阅文档

You can do:

你可以做:

var myDate = new Date(myDateAsString);

回答by David Hoerster

There are a few suggestions in the answers to this post -- How do I format a Microsoft JSON date?

这篇文章的答案中有一些建议——如何格式化 Microsoft JSON 日期?

If those examples aren't working, you can just format it manually. Here's a quick fiddle that demonstrates it -- http://jsfiddle.net/dhoerster/KqyDv/

如果这些示例不起作用,您可以手动对其进行格式化。这是一个演示它的快速小提琴 - http://jsfiddle.net/dhoerster/KqyDv/

$(document).ready(function() {
    //set up my JSON-formatted string...
    var myDate = new Date(2011,2,9);
    var myObj = { "theDate" : myDate };
    var myDateJson = JSON.stringify(myObj);

    //parse the JSON-formatted string to an object
    var myNewObj = JSON.parse(myDateJson);

    //get the date, create a new Date object, and manually format the date string
    var myNewDate = new Date(myNewObj.theDate);
    alert(myNewDate.getDate() + "/" + (myNewDate.getMonth()+1) + "/" + myNewDate.getFullYear());
});