Start a conversation

Extract only date or time from date and time field

If you want to extract a part of a date and time field (i.e. the date only, or the time only), you can use the following templates. Replace {your_datetime_variable} with your own variable name.

To extract a date from a date and time field:

var splitted_datetime = '{your_datetime_variable}'.split(';');
if(/^((0?[1-9]|[12][0-9]|3[01])[-](0?[1-9]|1[012])[-](19|20)?[0-9]{2})*$/.test(splitted_datetime[0])){
splitted_datetime[0];
} else {
"Date has not been filled in";
}


To extract the time from a date and time field:

var splitted_datetime = '{your_datetime_variable}'.split(';');
if(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/.test(splitted_datetime[1])){
splitted_datetime[1];
} else if(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/.test(splitted_datetime[0])){
splitted_datetime[0];
} else {
"Time has not been filled in";
}


Using the extracted date or time

The codes above, first split the field to separate date and time. Then, it extracts only the item [0] (date) or [1] (time). 

We can use this same process if we want to extract the date or the time to use it in another calculation instead of just showing it separately.

For example, we can have two date and time fields, one for the admission of the patient in the hospital and another one for the discharge. If we want to know the amount of complete days spent in the hospital we do not need the time, just the dates. We can then combine the code in the article Calculate the difference between two dates with the one described here as follows:

var datetime1 = '{your_datetime_variable1}'.split(';');
var datetime2 = '{your_datetime_variable2}'.split(';');
getDateDiff(datetime2[0],datetime1[0],'d')
Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Castor Support Team

  2. Posted
  3. Updated