Using Dates with JavaScript

Overview

Date objects are created with new Date() and used to work with dates in JavaScript.  Once a date object is created, a number of methods allow operations with the result.

All dates are calculated in milliseconds from 01 January, 1970 with a day containing 86,400,000 milliseconds

Examples

Current date and time

var todayDate = new Date();

Calculating the number of days between two dates

var dayStart = new Date(#start_date);

var dayEnd = new Date(#end_date);

var numberDays = (dayEnd - DayStart)/86400000;

Calculating the number of months between two dates

var dayStart = new Date(#start_date);

var dayEnd = new Date(#end_date);

var numberDays = (dayEnd - DayStart)/86400000;

var numberMonths = numberDays/30.4;

Adding days to a date

var myDate = new Date(#start_date);

myDate.setDate(myDate.getDate() + 7);

var myDatePlus7 = new Date(myDate);

Check that a date is not in the future

var myDate = new Date(#start_date);

var todayDate = new Date();

if (!VALIDATING && myDate > todayDate) alert("The start date is in the future");

Check if two dates are equal

var myDate1 = new Date(#date1);

var myDate2 = new Date(#date2);

if (!VALIDATING) {

  if  (myDate1 - myDate2 == 0) alert("The dates are equal");

}

The if (!VALIDATING) is included in the examples above due to the alert.  The code is validated by running it on the server.  The server does not have browser elements thus if the code references any browser elements the code needs to be blocked from executing during the validation.  An alert is a browser function and without the if (!VALIDATING) an error will occur.  When the code is actually run then the code within the if (!VALIDATING) will get executed. the code is run on the Preview tab and during data entry. 

 

Extracting parts of the date

var myDate = new Date(#date1);

var myMonth = myDate.getDate();

var myDay = myDate.getDay();

var myYear = myDate.getFullYear();

Copying a date

if (!VALIDATING) {

   var copyDate = new Date(#date1).toShortDateString();

   #date2 = copyDate;

}

.toShortDateString() is used to extract the date only as new Date() returns a date and time

Working With Dates in Datasets

Calculated Variables

After choosing the "Variables to include" (i.e., assigns a Vx alias to variable), some examples include:

var myMonth = v1.getMonth();

var myYear = v1.getFullYear();

 

Setting a date to midnight

Using new Date() gives the current date and time.  Some system dates, however, are set to midnight (e.g. Encounter Expected Date).  If comparing dates or using dates in calculations and need to set a date to midnight, use the following:

   var todayDate=new Date();

   todayDate.setHours(0,0,0,0); 

Months between two dates

function getMonthsBetween(date1,date2,roundUpFractionalMonths) { 
//Months will be calculated between start and end dates.
//Make sure start date is less than end date.
//But remember if the difference should be negative.
var startDate=date1;
var endDate=date2;
var inverse=false;
if(date1>date2) {
startDate
=date2;
endDate
=date1;
inverse
=true;
}

//Calculate the differences between the start and end dates

var yearsDifference=endDate.getFullYear()-startDate.getFullYear();
var monthsDifference=endDate.getMonth()-startDate.getMonth();
var daysDifference=endDate.getDate()-startDate.getDate();
var monthCorrection=0;

//If roundUpFractionalMonths is true, check if an extra month needs to be added from rounding up.
//The difference is done by ceiling (round up), e.g. 3 months and 1 day will be 4 months.
if(roundUpFractionalMonths===true && daysDifference>0) {
monthCorrection
=1;
} //If the day difference between the 2 months is negative, the last month is not a whole month.
else if(roundUpFractionalMonths!==true && daysDifference<0) {
monthCorrection
=-1;
}

return (inverse?-1:1)*(yearsDifference*12+monthsDifference+monthCorrection); };