Domanda

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.

È stato utile?

Soluzione

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));

Altri suggerimenti

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 comment/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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top