Purpose
Calculate a time difference

For two given times in the form "dd.mm.yyyy HH:MM", e.g. "05.02.2021 23:00" and "06.02.2021 07:15" we want to calculate the time difference in minutes.

For the sake of completeness, we first check the time data for plausibility using regular expressions. On the Internet you can find many collections of regular expressions for different tasks.

Solution

// Test data, format dd.mm.yyyy HH:MM

Set V[z1] "05.02.2021 23:00"

Set V[z2] "06.02.2021 07:15"


// plausibility checks for date and time

// date 1

Set V[x] "&V[z1](1-10)"   regex="^(((0|1)[0-9]|2[0-9]|3[0-1]).(0[1-9]|1[0-2]).((19|20)\d\d))$"

if not Q[ok]

  Message "E: Invalid date in &V[z1]" -statusline

  goto end

endif

// date 2

Set V[x] "&V[z2](1-10)"   regex="^(((0|1)[0-9]|2[0-9]|3[0-1]).(0[1-9]|1[0-2]).((19|20)\d\d))$"

if not Q[ok]

  Message "E: Invalid date in &V[z2]" -statusline

  goto end

endif

//time 1

Set V[x] "&V[z1](12-16)"   regex="^([0-1][0-9]|[2][0-3]):([0-5][0-9])$"

if not Q[ok]

  Message "E: Invalid time in &V[z1]" -statusline

  goto end

endif

// time 2

Set V[x] "&V[z2](12-16)"   regex="^([0-1][0-9]|[2][0-3]):([0-5][0-9])$"

if not Q[ok]

  Message "E: Invalid time in &V[z2]" -statusline

  goto end

endif

// calculate date difference in minutes

Set V[diff1] &V[z2](1-10) - &V[z1](1-10)

Set V[diff1] &V[diff1] * 1440   // 24*60 minutes per day

// calculate time difference in minutes

Set V[m1] &V[z1](12-13) * 60

Set V[m2] &V[z2](12-13) * 60

Set V[m1] &V[m1] + &V[z1](15-16)

Set V[m2] &V[m2] + &V[z2](15-16)

Set V[diff2] &V[m2] - &V[m1]

// total difference

Set V[diff] &V[diff1] + &V[diff2]

// display result

Message "S: Difference in minutes: &V[diff]" -statusline

label end



 

Components
InputAssistant