这个datetime格式是否有简单的转换? [英] Is there a simple conversion for this datetime format?

查看:115
本文介绍了这个datetime格式是否有简单的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery从JSON Feed中检索数据,作为Feed的一部分,我收到datetime属性,如2009-07-01 07:30:09。我想将这些信息放入一个javascript Date对象中以便于使用,但是如果我只是将其插入到构造函数中,那么Date对象就不会认为这种格式。有没有一个功能,也可能是一个聪明的技巧,可以用来快速将这种格式分解成Date对象可以识别和使用的东西。

解决方案

从该webservice检索的date属性不是一个真正的 Date ,因为它不是一个公认的日期格式。



将其处理为Date对象的最简单的方法是用T替换空白空间:

  var receivedDate =2009-07-01 07:30:09; 
var serializedDate = new Date(receivedDate.replace(,T));
alert(serializedDate);

这不是最正确的,因为它不处理时区,但在大多数情况下都可以工作。


I'm retrieving data from a JSON feed using jQuery and as part of the feed I'm getting 'datetime' attributes like "2009-07-01 07:30:09". I want to put this information into a javascript Date object for easy use but I don't believe the Date object would recognize this kind of format if I simply plugged it into the constructor. Is there a function or maybe a clever trick I can use to quickly break down this format into something the Date object can recognize and use?

解决方案

The "date" attribute you are retrieving from that webservice is not a real Date, as it is not a recognized date format.

The easiest way to handle it as a Date object would be to replace the empty space with a "T":

var receivedDate = "2009-07-01 07:30:09";
var serializedDate = new Date(receivedDate.replace(" ", "T"));
alert(serializedDate);

This is not the most correct, as it is not handling timezones, but in most cases will work.

这篇关于这个datetime格式是否有简单的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆