Logic Machine Forum
Subtract 30 minutes from time value - 2021-05-12T19:30:00 - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Subtract 30 minutes from time value - 2021-05-12T19:30:00 (/showthread.php?tid=3365)



Subtract 30 minutes from time value - 2021-05-12T19:30:00 - jamesng - 12.05.2021

Hi

I receive time values from an API in the following format

2021-05-12T19:30:00

How can I subtract 30 minutes from the time (and also correct for date for when I receive midnight value?)

Many thanks in advance

James


RE: Subtract 30 minutes from time value - 2021-05-12T19:30:00 - admin - 12.05.2021

Use this:
Code:
str = '2021-06-01T00:00:00'

date = {}

-- assign table keys for further conversion
date.year, date.month, date.day, date.hour, date.min, date.sec =
  str:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')

-- convert to unix timestamp and subtract 30 minutes in seconds
timestamp = os.time(date) - 30 * 60

-- convert timestamp to table
date = os.date('*t', timestamp)

log(date)



RE: Subtract 30 minutes from time value - 2021-05-12T19:30:00 - jamesng - 12.05.2021

Hi

This seems to return the date as a table.

Code:
* table:
[sec]
  * number: 0
[min]
  * number: 0
[day]
  * number: 12
[isdst]
  * bool: false
[wday]
  * number: 4
[yday]
  * number: 132
[year]
  * number: 2021
[month]
  * number: 5
[hour]
  * number: 19


How can I convert this back to a string in the same format as the original time (ie: 2021-05-12T19:30:00 > 2021-05-12T19:00:00)?

Many thanks
James


RE: Subtract 30 minutes from time value - 2021-05-12T19:30:00 - admin - 12.05.2021

Code:
str = '2021-06-01T00:00:00'

date = {}

-- assign table keys for further conversion
date.year, date.month, date.day, date.hour, date.min, date.sec =
  str:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')

-- convert to unix timestamp and subtract 30 minutes in seconds
timestamp = os.time(date) - 30 * 60

-- convert timestamp to string
result = os.date('%Y-%m-%dT%H:%M:%S', timestamp)
log(result)



RE: Subtract 30 minutes from time value - 2021-05-12T19:30:00 - jamesng - 12.05.2021

This worked perfectly! Many thanks