admin管理员组文章数量:1397192
Doing some data wrangling with a large dataset. The data has a "date" field that randomly switches between a format like "1370039735000" and "May 16, 2013". So far I've converted other date fields with either
new Date("May 16, 2013")
or
new Date(NumberLong(1370039735000))
How can I tell the difference between the two using regex or some other means? I'm using MongoDB but it's all Javascript.
Doing some data wrangling with a large dataset. The data has a "date" field that randomly switches between a format like "1370039735000" and "May 16, 2013". So far I've converted other date fields with either
new Date("May 16, 2013")
or
new Date(NumberLong(1370039735000))
How can I tell the difference between the two using regex or some other means? I'm using MongoDB but it's all Javascript.
Share Improve this question edited Dec 27, 2013 at 19:40 dee-see 24.1k6 gold badges62 silver badges91 bronze badges asked Dec 27, 2013 at 19:33 JulianJulian 1,8315 gold badges29 silver badges49 bronze badges 2-
1
Can you not use MongoDB's query
$type
operator to check if it's a date or timestamp field? – hwnd Commented Dec 27, 2013 at 20:02 - @hwnd I couldn't use $type because all the data es to me as a String. – Julian Commented Dec 27, 2013 at 20:34
3 Answers
Reset to default 5If it's a unix timestamp, it's numbers only, and if it's not, it's an actual string (not empty or boolean) and javascript has a function for that, isNaN
isNaN(datestring_or_number)
can be easily used
new Date(isNaN(str) ? str : NumberLong(str));
From your post, I assume you are absolutely 100% certain that these are the only two possible formats. If it is not the case then ment/edit your post.
Testing if the date contains a letter should seal the deal in a simple manner.
/[a-z]/i.test("May 16, 2013") // true, the date is written out
/[a-z]/i.test(1370039735000) // false, it's unix epoch format
If you're worried about speed, you should just test against the Regex "^\D" (not a digit) because as soon as it hits the "M" in "May..." it will fail. This will fail quickly and only run the minimum amount of times. You might also consider just getting a substring of the first character in the string and trying to convert it to an int and if it fails, then the same thing is true. By keeping the Regex short like that however, speed shouldn't be an issue.
本文标签:
版权声明:本文标题:regex - Determine if string format is "May 16, 2013" or UNIX Timestamp with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744148603a2592956.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论