Home

Date Manipulation

NOTE...
var nowDate = Date();
var orgDate = now;
…ANY changes you make to orgDate will ALSO be made to nowDate as Date() is an object and therefore so will be nowDate and orgDate.
The two will be linked as objects come across byReference as opposed to byValue.

var nowDate = new Date();
var orgDate = new Date();

will create two separate instances so they will NOT be linked.

Reading In Dates

To get TODAY's DATE
var now = new Date();

This will give the variable 'now' the following value

And then you can retrieve what you need in various ways...eg... (using the above date & time)
& see the answer below...



(returns 0-11 so you must add 1 for 1-12)



(24hr)







(returns No. milliseconds since Jan 1st, 1970)

(returns weekday as a number 0 (Sun) - 6 (Sat))

(returns HH:MM:SS am/pm)

(returns DD/MM/YYYY)

(returns DD/MM/YYYY, HH:MM:SS am/pm)

Setting Dates

Where 'now' has the following value

Note...setting the date changes itself directly (will change variable 'now'
so I created a new variable called 'd' and set that to be the same as 'now'
(Note...I had to use the getTime() function of now and use that to create d...search for 'keepDate' in the code)
then I can change the value of 'd' without losing the value of 'now')

Click a button - results will be shown below
(sets year to be 2050)

(sets date to be 19th June, 1957)

(Jan=0, Feb=1, Mar=2, Apr=3 etc...this sets it to 5(June))

(sets date to be 7th)

(sets to be 2pm (1400 hours))

(sets to be 15 mins)

(sets to be 25 seconds)

(this will add 75 minutes)

(this will add 28 days)

(checks if today > 19th June, 1957)

Calculate AGE from Date of Birth

We need to pass the function the Date of Birth then we can calculate it...
function calculateAge (birthDate) {
   birthDate = new Date(birthDate);
   todayDate = new Date();
   var years = (todayDate.getFullYear() - birthDate.getFullYear());
   if todayDate.getMonth() < birthDate.getMonth() ||
      todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() < birthDate.getDate()) {
      years--;
   }    return years; }


Calling this function will return an age in years (it allows for leap years).

For testing I will pass the Date of Birth as "06/19/1957" (MM/DD/YYYY) to get back...

Converting todays date to YYYYMMDD
and YYYY-MM-DD then do this...

 
var todaysDate = new Date();

function convertDate(date, format) {
  var yyyy = date.getFullYear().toString();
  var mm = (date.getMonth()+1).toString();
  var dd  = date.getDate().toString();

  var mmChars = mm.split('');
  var ddChars = dd.split('');

  if (format === 1) {
    return yyyy + 
        (mmChars[1]?mm:"0" +
        mmChars[0]) + 
        (ddChars[1]?dd:"0" +
        ddChars[0]);
  } else {
    return yyyy + '-' + 
        (mmChars[1]?mm:"0" + mmChars[0]) + '-' + 
        (ddChars[1]?dd:"0" + ddChars[0]);                
  }
}

console.log(convertDate(todaysDate, 0));  
    // Returns: YYYY-MM-DD
console.log(convertDate(todaysDate, 1));  
    // Returns: YYYYMMDD

Date in format 0 (YYYY-MM-DD) is:

Date in format 1 (YYYYMMDD) is: