admin管理员组文章数量:1416622
From server, I am getting valid date for some values in object, But I don't know for which field I will get number and for which field I will get date.
I tried with this question and its answers, It was giving correct result for string and dates, but now It is treating number as date. Any other solution to avoid treating number as date?
This question is to parse the date in different format. And my question is about distinguishing between date and number string. And performing operations on only date strings.
I am getting date in yyyy-MM-ddThh:mm:ss+Z.
From server, I am getting valid date for some values in object, But I don't know for which field I will get number and for which field I will get date.
I tried with this question and its answers, It was giving correct result for string and dates, but now It is treating number as date. Any other solution to avoid treating number as date?
This question is to parse the date in different format. And my question is about distinguishing between date and number string. And performing operations on only date strings.
I am getting date in yyyy-MM-ddThh:mm:ss+Z.
Share Improve this question edited May 23, 2017 at 12:29 CommunityBot 11 silver badge asked Aug 31, 2015 at 12:58 Laxmikant DangeLaxmikant Dange 7,7186 gold badges43 silver badges67 bronze badges 6-
2
What do you mean by "is a date?" Do you mean matches a particular format? Which format? Why not just use that answer with a switch on
typeof
? – Mike Samuel Commented Aug 31, 2015 at 13:02 - 1 If you get the value 2015, than it could be the number 2015 (integer) or the year 2015 and thus a date. You need some criteria to distinguish numbers from dates since they can be both under certain circumstances. – cezar Commented Aug 31, 2015 at 13:04
- 1 What is "GMT" format? GMT is a timezone, not a format. – deceze ♦ Commented Aug 31, 2015 at 13:05
- possible duplicate of javascript Date.parse – Rahul Tripathi Commented Aug 31, 2015 at 13:06
- @cezar, Yes thats the problem, I can't distinguish between date and time, Sometime date is with time and for some keys it is without time. – Laxmikant Dange Commented Aug 31, 2015 at 13:09
1 Answer
Reset to default 2Got the solution
Instead of checking if it is date or not, I need to check first if it is number or not. If it is not number then only it can be a date.
Here is What I did and it worked.
var obj={
key1:"2015-10-10T11:15:30+0530",
key2:2015,
key3:"Normal String"
}
function parseDate(dateStr){
if(isNaN(dateStr)){ //Checked for numeric
var dt=new Date(dateStr);
if(isNaN(dt.getTime())){ //Checked for date
return dateStr; //Return string if not date.
}else{
return dt; //Return date **Can do further operations here.
}
} else{
return dateStr; //Return string as it is number
}
}
console.log("key1 ",parseDate(obj.key1))
console.log("key2 ",parseDate(obj.key2))
console.log("key3 ",parseDate(obj.key3))
本文标签: Check if string is date or not in JavaScriptStack Overflow
版权声明:本文标题:Check if string is date or not in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256299a2650121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论