Calculating the difference between times on different dates

Calculating the time duration between a start and end date can be used when analysing your survey responses. This could be used to show how long someone spent doing a particular task, had to wait to be served or how long a journey took.

You can use the date and time information directly in analyses, or use a further derived variable to break it down into ranges defined by time intervals, for example, short journey, medium journey, and long journey. By using these two methods, you can present the same data in quantitative and qualitative form, allowing for different types of analyses.

Snap XMP Desktop provides functions to calculate the differences between dates and times. The differences between dates are given in days, and the differences in times are given in seconds.

This tutorial shows how to create time and date questions that are used to calculate the duration of a journey.

Step 1: Creating the time and date questions

  1. Click Survey Overview SurveyOverviewIcon.png on the Snap XMP Desktop toolbar to open the Survey Overview window.
  2. Open the questionnaire in the Questionnaire window.
  3. Click New QuestionNewSurveyIcon.png to create the start date question.
    • Set the style to Open Series First.
    • Enter the question text “Please enter the date and time that you left home”.
    • Enter the grid label text “Date of leaving”.
    • Set the response type to Date.
ResponseDate.PNG
  1. Press Tab to create a new row in the question.
    • Enter the grid label text “Time of leaving”.
    • Set the response type to Time.
  2. Repeat steps 3 and 4 to create the arrival date and time questions, changing the text as appropriate.
DateQuestions.PNG
  1. Click Save SaveIcon.png  .

Step 2: Creating derived variables for the dates and times

The date and time variables you have created use the question numbers as the name by default. The dates and times will be used in a formula and it is clearer to create derived variables using a name that explains the purpose of each date and time. This step describes how to create derived variables for the dates and times entered in the questionnaire.

  1. Click Variables VariablesIcon.png  on the Snap XMP Desktop toolbar. This opens the Variables window.
  2. Click New VariableNewSurveyIcon.png  to create a new variable. This opens the Variable Details window.
    • In Name, enter the name of the variable, LeaveDate.
    • In Label, enter the description, Date of leaving.
    • In the Code list, enter the question number, in this case Q1a, in the values column of the row OK.
LeaveDateDV.PNG
  1. Click Save SaveIcon.png  to save the variable.
  2. Repeat steps 2 and 3 for
    • Q1b the Time of leaving with name LeaveTime and values Q1b
    • Q2a the Date of arrival with name ArriveDate and values Q2a
    • Q2b the Time of arrival with name ArriveTime and values Q2b

Note: You can change the question name but this is displayed in place of the question number. This could look confusing unless the question numbers are hidden.

Step 3: Creating the derived variable to calculate the difference

The next step creates a derived variable to calculate the difference between the time that the participant left home and the time they arrived at their destination.

  1. Click Variables VariablesIcon.png  on the Snap XMP Desktop toolbar. This opens the Variables window.
  2. Click New VariableNewSurveyIcon.png  to create a new variable. This opens the Variable Details window.
    • In Name enter the name of the variable journeyTime
    • In Label, enter Difference between leaving and arrival time in hours
    • In Type, select Derived
    • In Response, select Quantity.
journeyTime.PNG
  1. In Code list, enter the formula in Values in the row OK.
    • The first part of the formula is working out the days spent on the journey and converting it to hours by multiplying it by 24.
      (ArriveDate-LeaveDate)*24
    • The second part of the formula is the difference between the two times. In Snap XMP Desktop, subtracting one time from another always produces a positive value. If the second time is before the first, it is assumed that one must be before midnight and one must be after midnight, and works out a time across midnight. This means that 10am – 9pm gives 13 hours rather than -11 hours. The time differences are in seconds. To convert the time difference to hours, you must divide it by 3600
      (ArriveTime-LeaveTime)/360
    • The last part of the formula adjusts for when the time of arriving is less than time of leaving, for example when a journey begins at 9pm on one day and finishes at 10 am on the next day. This gives the logical expression (ArriveTime < LeaveTime). The num function is used to convert the true or false answer to a numeric value, with 1 for true and 0 for false. Then multiply this result by 24, gives the right number of hours to take away, 0 if ArriveTime isn’t smaller than LeaveTime, and 24 if it is.(24 *num(ArriveTime < LeaveTime))
    • So the whole formula reads: ( ArriveDate – LeaveDate)*24 + (ArriveTime – LeaveTime)/3600 – 24*num(ArriveTime < LeaveTime)
  2. Click Save SaveIcon.png  to save the variable.

Step 4: Creating bands for time duration ranges used in analysis

  1. Click New VariableNewSurveyIcon.png  to create a new variable. This opens the Variable Details window.
    • In Name enter the name of the variable journeyType
    • In Label, enter Banded journey lengths
    • In Type, select Derived
    • In Response, select Single.
  2. In the Code list enter the values:
    • For code 1, enter the label Short journey, and the value as journeyTime<=2. All cases where the time taken is less than or equal to two hours are stored as Quick trips.
    • For code 2, enter the label Medium journey, and the value as journeyTime <=6 as the value. All cases where the time taken is 2 to 6 hours are stored as Medium trips.
    • For code 3, enter the label Long journey, and the value as journeyTime >6 as its values. All cases where the time taken is greater than six hours are stored as Long trips
journeyType.PNG

You can now perform analyses using either the journeyType or the journeyTime variable. For example:

Analysis.PNG

If there is a topic you would like a worksheet on, email to snapideas@snapsurveys.com

Contents