0

I have a registration form that asks the user for their start date. I am using a MEAN Framework which means Im using AngularJs on the front end.

What I have done so far:

First I tried using the following code BUT it does not work on IE & FireFox.

<input type="date" name="startDate">

Then I tried the following REGEX that I found on this SO question.

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

The works perfectly. Below is an image of how it works in my app.

enter image description here

The Issue:

When the user submits the form the data is saved in MongoDB. In the above image you can see that data is saved as a string. I want to convert it to a proper Date format so I can perform actions on it server side (Node.js) i.e filter all users who started after 2015 or left between 1999 & 2001 etc.

Any suggestions would be helpful.

UPDATE:

Currently I am testing moment.js. Will update my question once I have a result.

Result:

I tried the following using moment.js:

console.log("MOMENT" + moment("1993-03-25").format());

The output of the following was:

MOMENT 1993-03-25T00:00:00+00:00

I am not sure whether this is the correct format to be saved in MongoDB and will it allow me to perform server side actions on it.

Community
  • 1
  • 1
Skywalker
  • 4,984
  • 16
  • 57
  • 122
  • This is from the mongo docs `Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.` – Aukhan Mar 01 '16 at 10:39

1 Answers1

0

You need to convert your moment object to a JavaScript date before you can save it MongoDB as a date field:

moment("1993-03-25").toDate()
Alex
  • 21,273
  • 10
  • 61
  • 73
  • Thank you so much for the answer. The above code outputs the following format `Thu Mar 25 1993 00:00:00 GMT+0000 (GMT)`. Is this the correct format to be saved in? – Skywalker Mar 01 '16 at 10:46
  • Yes, that is the correct format. It is in fact a JavaScript date object, however, Mongo knows how to handle these. – Alex Mar 01 '16 at 11:25