获取日期和/或在 JSON 对象中检查它?
我有一个 JSON 对象,可以存储 Date (和/或任何其他 JS 对象),但我不知道如何将其取出。例如,我有这个示例代码,但它不起作用:
for(var key in value){
if(value.hasOwnProperty(key)){
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
但是,它只是保持返回不是日期
。如果我使用 Firebug 或 WebKit 中的开发者控制台检查 JSON 对象,我会在相应的 JSON 项中看到 __proto__: Date
...那么,我该如何取出或检查它呢?
--编辑--
这是我在调试器中看到的内容:
Object
->isADate: Fri Nov 26 2010 20:30:57 GMT-0800 (Pacific Standard Time)
--->__proto__: Date
->notADate: "some string"
--->__proto__: Object
这是我正在创建的 JSON:
var dateStorage = new storageLocker('date-test');
dateStorage.save({'isADate':new Date(),'notADate':'some string'});
这是我的脚本这部分的代码 (http://github.com/oscargodson/storagelocker)
storageLocker.prototype.save = function(value){
var json = JSON.parse(localStorage.getItem(this.catalog));
if(json == null){json = {};}
for(var key in value){
if(value.hasOwnProperty(key)){
json[key] = value[key];
console.log(value[key].Date);
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
localStorage.setItem(this.catalog,JSON.stringify(json));
return this;
}
非常感谢!希望这可以帮助更多人!
I have a JSON object that can (is) storing a Date (and/or any other JS object) but I'm not sure how to get it out once it's in it. For example I have this sample code, but it's not working:
for(var key in value){
if(value.hasOwnProperty(key)){
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
However, it just keeps return not a date
. If i inspect the JSON object with Firebug or the Developer Console in WebKit i see __proto__: Date
inside of the corresponding JSON item... so, how do I get it out or check for it?
--EDIT--
Here is what i see in the debugger:
Object
->isADate: Fri Nov 26 2010 20:30:57 GMT-0800 (Pacific Standard Time)
--->__proto__: Date
->notADate: "some string"
--->__proto__: Object
And here is the JSON im creating:
var dateStorage = new storageLocker('date-test');
dateStorage.save({'isADate':new Date(),'notADate':'some string'});
Here is the code for this part of my script (http://github.com/oscargodson/storagelocker)
storageLocker.prototype.save = function(value){
var json = JSON.parse(localStorage.getItem(this.catalog));
if(json == null){json = {};}
for(var key in value){
if(value.hasOwnProperty(key)){
json[key] = value[key];
console.log(value[key].Date);
if(typeof value[key] == 'object'){
if(value[key].Date){
console.log('this is a date')
}
else{
console.log('not a date');
}
}
}
}
localStorage.setItem(this.catalog,JSON.stringify(json));
return this;
}
Thanks a lot! Hope this helps out more!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想看看这个: 如何使用 JavaScript 知道对象是否是日期
基本上,“最安全”(不是 100% 故障安全,但应该没问题)是进行特征检测。
但是,请确保您的 JSON 条目确实是日期对象而不是字符串,在这种情况下,这将不起作用,您需要使用
new Date()
或Date .parse()
使其成为日期对象。编辑:根据您的 2 条评论:
JSON 不允许您以日期格式存储对象。所以,这里已经存在差异了。要么您没有真正处理 JSON 对象,要么您没有按照应有的方式处理它们。
请参阅此页面,了解有关使用 JavaScript 中的 JSON 的官方文档,了解有关如何使用 reviver 的信息JSON.parse 的参数,因为日期应该存储为字符串,然后“恢复”为 Date 对象。
另外, typeof 返回 object 并不一定意味着您不处理字符串,例如:
现在假设您此时确实有一个 Date 对象,我不知道如何测试
value[ key].Date
无论如何都会起作用。在调试控制台中尝试一下,因为您有 FireBug:因此,结合使用
getMonth
、getDay
和其他测试可以达到目的。You want to look at this: How to know if an object is a date or not with JavaScript
Basically the "safest" - not 100% failsafe but you should be fine - is to do feature detection.
However, make sure that your JSON entry is really a date object and not a string, in which case this won't work and you'll need to use the
new Date()
orDate.parse()
to make it a date object.EDIT: Following your 2 comments:
JSON does not allow you to store objects in a Date format. So, there's already a discrepancy here. Either you're not really dealing with JSON objects, or you're not dealing with them the way they should be.
See this page for the official documentation on using JSON in JavaScript for information on how to use the reviver parameter of JSON.parse, because a date should be stored as a string and then "revived" to a Date object.
Also, that typeof returns object doesn't necessarily mean that you don't deal with a string, for instance:
Now assuming you really do have a Date object at this point, I don't see how testing for
value[key].Date
would work anyway. Try it out in your debug console, as you have FireBug:So using a combination of tests for
getMonth
,getDay
and others would do the trick.如果您和我一样,正在寻找一种方法来发现 JSon 日期值等于什么日期/时间,您可以按如下方式执行此操作(使用 wscript - Windows 脚本主机):
将以下文本保存到文件中名为 test.js(将 1396566000000 替换为您自己的日期值)
var 日期 = 新日期(1396566000000);
WScript.Echo(date);
从命令提示符处运行:
wscript test.js
If, like me, you got here looking for a way to discover what date/time a JSon date value equals, you can do this as follows (using wscript - the Windows script host):
save the following text to a file called test.js (replace 1396566000000 with your own date value)
var date = new Date(1396566000000);
WScript.Echo(date);
from a command prompt, run:
wscript test.js
我发现这个非常好的示例如何转换日期时间存储在 JSON 中的字符串返回 Date 对象。
但在此示例中日期时间常量并不完整,更好的是
I found this very nice example how to convert datetime strings stored in JSON back to Date objects.
But datetime constant is not full in this example, better is