John Cooper
John Cooper

Reputation: 7651

Parsing date object in JavaScript

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.

Upvotes: 0

Views: 144

Answers (3)

Jan
Jan

Reputation: 2293

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)

Upvotes: 5

Wladimir Palant
Wladimir Palant

Reputation: 57691

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.

Upvotes: 2

oneat
oneat

Reputation: 10994

Try using these three:

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

Upvotes: 1

Related Questions