0
var formattedDate = new Date(parseInt(thisObj.Patient.DateOfBirth.substr(6)));

When i print my Date Object, i get this as output.

Wed May 04 2011 09:30:00 GMT+0530 (GMT+05:30)

How can i separate May, 04 and 2011 into separate variables like

var Month = May;
var Date = 04;
var Year = 2011;

How i can also check whether the Age of the person is below one year or not.

1
  • Using integrated IDEs (like netbeans) can help you in finding methods of lots of objects. Commented Jun 25, 2011 at 12:31

3 Answers 3

5

The object has many handy methods - use them.

Date Object on w3schools

  • getDate(): returns the day of the month (from 1-31)
  • getFullYear(): returns the year (four digits)
  • getMonth(): returns the month (from 0-11)
Sign up to request clarification or add additional context in comments.

Comments

2

You can only access the numerical values, you have to format them yourself then. Use Date.getFullYear(), Date.getMonth() etc. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date for documentation of this object.

You can compare dates by subtracting them:

alert(new Date() - formattedDate);

This will show the number of milliseconds between current date and formattedDate. Now you only need to know the number of milliseconds in a year.

Comments

1

Try using these three:

formattedDate.getYear() .getMonth() .getDay()

1 Comment

getYear is deprecated (and has been for many years), you should use getFullYear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.