JSON 日期格式 mm/dd/yyyy

发布于 2024-10-04 18:15:04 字数 649 浏览 0 评论 0原文

这是我得到的数据:

jsonp1290537545248( [{"Active":true,"EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400) \/"}]);

  $.getJSON(url, {},
      function (data) {
          alert(data[0].EndDate);
          alert(Date(data[0].StartDate));
          //alert(data[0].StartDate.getDate());// + "/" + (data[0].StartDate.getMonth() + 1) + "/" + data[0].StartDate.getFullYear()); // alerts: "15/10/2008" 
          //var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
          alert('dd    ' + new Date(parseInt(data.substr(6)))); 

      });

如何获取 MM/DD/YYYY 格式?

Here is what I am getting as data:

jsonp1290537545248( [{"Active":true,"EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400)\/"}] );

  $.getJSON(url, {},
      function (data) {
          alert(data[0].EndDate);
          alert(Date(data[0].StartDate));
          //alert(data[0].StartDate.getDate());// + "/" + (data[0].StartDate.getMonth() + 1) + "/" + data[0].StartDate.getFullYear()); // alerts: "15/10/2008" 
          //var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
          alert('dd    ' + new Date(parseInt(data.substr(6)))); 

      });

How do I get in the MM/DD/YYYY format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一个人练习一个人 2024-10-11 18:15:04

我会使用与 Zain 发布的类似的正则表达式,但不会像这样使用 eval() (演示):

var start = parseInt(data.StartDate.replace(/\/Date\((.*?)[+-]\d+\)\//i,"$1"), 10),
    date = new Date( start ),
    fStart = date.getMonth()+1 + '/' + date.getDate() + '/' + date.getFullYear();

结束日期是哪一天?这似乎没有什么区别,如果您使用该数字作为新日期,您最终会得到“Sun Dec 31 0000 22:59:59 GMT-0600(中部标准时间)”...所以我不是确定如何处理该值。

I would use a similar regex to what Zain posted, but not use eval() like this (demo):

var start = parseInt(data.StartDate.replace(/\/Date\((.*?)[+-]\d+\)\//i,"$1"), 10),
    date = new Date( start ),
    fStart = date.getMonth()+1 + '/' + date.getDate() + '/' + date.getFullYear();

And what is that end date? It doesn't seem to be a difference and if you use that number as a new date you end up with "Sun Dec 31 0000 22:59:59 GMT-0600 (Central Standard Time)"... so I wasn't sure what to do with that value.

无戏配角 2024-10-11 18:15:04

在确定日期时有必要考虑时区。我假设日期的第一部分是 Java 或 JavaScript 的 Date.getTime() 的输出(自 1970 年 1 月 1 日 00 点以来的毫秒数: 00:00 世界标准时间)。

为了正确输出日期上的所有时间,有必要在创建 Date 对象之前应用时区偏移量(例如 -0500 表示东部标准时间),然后使用UTC 方法来获取部分日期。原因是 JavaScript 没有提供 Date.setTimezoneOffset() 方法来将时区设置为正确的时区(无法从访问者的系统时区更改它)。

代码示例

这是我想出的代码。它使用正则表达式提取编码日期的各个部分,应用指定的时区偏移量,创建一个 Date 对象,然后从这些部分构建一个日期(演示:http://jsfiddle.net/Wa8LY/1/)。

var dateParts = data[0].StartDate.match(/\((.*)([+-])(..)(..)\)/);
var dateObj = new Date(
    /* timestamp in milliseconds */ Number(dateParts[1]) +
    /* sign of timezone offset */ Number(dateParts[2] + '1') *
    /* hours and minutes offset */ (36e5 * dateParts[3] + 6e4 * dateParts[4])
);

var dateMMDDYYYY = [dateObj.getUTCMonth() + 1,
                    dateObj.getUTCDate(),
                    dateObj.getUTCFullYear()].join('/');

左填充组件

如果您需要左填充日期的组件(例如 01/01/0001),您可以使用此函数来帮助执行此操作:

function leftPadWithZeroes(str, len) {
    return (new Array(len + 1).join('0') + str).slice(-len);
}

并更改最后几行(演示:http://jsfiddle.net/5tkpV/1/):

var dateMMDDYYYY = [leftPadWithZeroes(dateObj.getUTCMonth() + 1, 2),
                    leftPadWithZeroes(dateObj.getUTCDate(), 2),
                    leftPadWithZeroes(dateObj.getUTCFullYear(), 4)].join('/');

It's necessary to consider the timezone when determining which date it is. I assume that the first part of the date is the output from Date.getTime() of Java or JavaScript (i.e. the number of milliseconds since January 1, 1970, 00:00:00 UTC).

For the correct output for all times on a date, it is necessary to apply the timezone offset (e.g. -0500 for Eastern Standard Time) before creating the Date object and then use the UTC methods to get parts of the date. The reason is that JavaScript does not provide a Date.setTimezoneOffset() method to set the timezone to the correct one (it's not possible to change it from the visitor's system timezone).

Code example

Here's the code I came up with. It uses a regex to extract the parts of the encoded date, applies the specified timezone offset, creates a Date object, and then builds a date from the parts (demo: http://jsfiddle.net/Wa8LY/1/).

var dateParts = data[0].StartDate.match(/\((.*)([+-])(..)(..)\)/);
var dateObj = new Date(
    /* timestamp in milliseconds */ Number(dateParts[1]) +
    /* sign of timezone offset */ Number(dateParts[2] + '1') *
    /* hours and minutes offset */ (36e5 * dateParts[3] + 6e4 * dateParts[4])
);

var dateMMDDYYYY = [dateObj.getUTCMonth() + 1,
                    dateObj.getUTCDate(),
                    dateObj.getUTCFullYear()].join('/');

Left padding the components

If you need to left pad the components of the date (e.g. 01/01/0001), you could use this function to help do so:

function leftPadWithZeroes(str, len) {
    return (new Array(len + 1).join('0') + str).slice(-len);
}

And change the last lines to (demo: http://jsfiddle.net/5tkpV/1/):

var dateMMDDYYYY = [leftPadWithZeroes(dateObj.getUTCMonth() + 1, 2),
                    leftPadWithZeroes(dateObj.getUTCDate(), 2),
                    leftPadWithZeroes(dateObj.getUTCFullYear(), 4)].join('/');
所谓喜欢 2024-10-11 18:15:04

这可能会有所帮助。请参阅 http://jsfiddle.net/zainshaikh/pysAR/ 上的演示。

var date = eval(data[0].StartDate.replace(/\/Date\((.*?)\)\//gi, "new Date($1)"));

然后您可以使用 JavaScript 日期格式 脚本(缩小后为 1.2 KB) gzipped)以按您想要的方式显示它。

如何设置 Microsoft JSON 日期的格式?

This might help. See the demo at http://jsfiddle.net/zainshaikh/pysAR/.

var date = eval(data[0].StartDate.replace(/\/Date\((.*?)\)\//gi, "new Date($1)"));

And then you can use the JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.

How do I format a Microsoft JSON date?

不可一世的女人 2024-10-11 18:15:04

自动将序列化 JSON 日期转换为实际 Javascript 日期

由于您使用的是 jQuery,您可能对我编写的自动将序列化日期转换为实际 Javascript 日期的代码感兴趣。

您的代码仍将在客户端上使用 $.parseJSON() ,但使用第二个参数告诉它自动转换日期。现有代码仍然有效,因为扩展功能仅根据您的需求解析日期。

查看博客文章并了解自己。它是可重复使用的,并且可以在全球范围内使用,因此您可以忘记这种手动日期转换。

Auto convert serialized JSON dates to actual Javascript dates

Since you're using jQuery, you might be interested in the code I've written that auto converts serialized dates to actual Javascript dates.

Your code would still use $.parseJSON() on the client but with the second parameter where you tell it to automatically convert dates. Existing code will still work, because extended functionality only parses dates on your demand.

Check blog post and find out yourself. It's reusable and will work globally so you could just forget about this manual date conversion.

断念 2024-10-11 18:15:04

以下内容有效,因为我的日期字符串是“/Date(1334514600000)\”

'function ConvertJsonDateString(jsonDate) {  
   var shortDate = null;    
   if (jsonDate) {  
       var regex = /-?\d+/;  
       var matches = regex.exec(jsonDate);  
       var dt = new Date(parseInt(matches[0]));  
       var month = dt.getMonth() + 1;  
       var monthString = month > 9 ? month : '0' + month;  
       var day = dt.getDate();  
       var dayString = day > 9 ? day : '0' + day;  
       var year = dt.getFullYear();  
       shortDate = monthString + '/' + dayString + '/' + year;  
}  
return shortDate;  
};'

The following worked because my datestring was "/Date(1334514600000)\"

'function ConvertJsonDateString(jsonDate) {  
   var shortDate = null;    
   if (jsonDate) {  
       var regex = /-?\d+/;  
       var matches = regex.exec(jsonDate);  
       var dt = new Date(parseInt(matches[0]));  
       var month = dt.getMonth() + 1;  
       var monthString = month > 9 ? month : '0' + month;  
       var day = dt.getDate();  
       var dayString = day > 9 ? day : '0' + day;  
       var year = dt.getFullYear();  
       shortDate = monthString + '/' + dayString + '/' + year;  
}  
return shortDate;  
};'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文