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
Add a ment  | 

3 Answers 3

Reset to default 5

If 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.

本文标签: