Working with dates in the script
Understanding the details
Script | Meaning |
---|---|
var oneDay = 1000*60*60*24; | The number of milliseconds in a day |
var startDateVariable = #someStartDate.value; | Object representing the Studytrax variable |
var startDate = startDateVariable.value; | JavaScript Date object |
An example
var startDate = #AEonsetDate.value;
var endDate = #AEendDate.value;
if (!isNull(startDate.value) && !isNull(endDate.value)) {
var AEdays = (endDate - startDate)/oneDay;
}
Output.write("The AE started on " + startDate.toString() + ", and lasted for " + AEdays + " days.");
Medication Collection Dates
Note that the dates in the MEDICATIONS collection is a string by default (easier to format in a subject report). However, must convert to date value if used in comparison (or use StartDateDate or EndDateDate). An example comparing dates is below, uses the string conversion method.
var medicationList = MEDICATIONS;
for(var index = 0; index < medicationList.Count; index++) {
var medication = medicationList.GetMedication(index);
var medName = medication.TradeName;
var medStartString = medication.StartDate; '// note this is a formatted string, the same as using #VariableName.toString()
'// thus for date comparisons e.g., >, <, >=, etc., must use the string to create at date variable (see below), which would be the same as #VariableName.value
var medStart = new Date(medication.StartDate)
var medEnd = new Date(medication.EndDate);
if (!isNull(medStart)) {
if (encDate >= medStart) {
if (encDate <= medEnd || isNull(medEnd)) {
do something...
}
}
}
}