This topic contains 4 replies, has 2 voices, and was last updated by Sandeep 2 years, 11 months ago.
Issue with date parse module
You must be logged in to reply to this topic.
This topic contains 4 replies, has 2 voices, and was last updated by Sandeep 2 years, 11 months ago.
Hi,
I am using date parser Lua to convert my date but I am getting this error:
unknown date/time: 2019-04-29 0615
Input date:2019-04-29 0615
Desired date: unknown date/time: 2019-04-29 06:15
Code:
local dateparse = require ‘date.parse’
function main()
local T = dateparse.parse(‘2019-04-29 0615’)
trace( os.date(‘%Y-%m-%d %H:%M’, T))
end
Please advise.
Thank you in advance.
The date.parse module doesn’t have a built-in template for the date format you’re using so you’ll have to use a custom one:
Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com
Thank you, Jeff. It worked. 🙂 Can I ask how can I get the difference of this time with the current time? I am using the below code but not sure if it is a correct way or not.
timeValue, timetable= dateUtil.convertTime(timeValue)
timeDiff= os.ts.difftime(os.ts.time(),os.ts.time(timetable))
function dateUtil.convertTime(date)
local dateObj, dateTable = dateParser.parse(date,’yyyy-mm-dd HHMM’)
trace(os.date(‘yyyy-mm-dd HH:MM’,dateObj))
return dateObj,dateTable
end
I would have done this. The example below assumes variable theDate has the string value ‘2019-04-29 0615’:
local theDateObj,theDateTbl = dateparse.parse(theDate,'yyyy-mm-dd HHMM') local theEpochTime = os.ts.time(theDateTbl) local timeDiffInSecs = math.abs(os.ts.time() - theEpochTime)
The variable timeDiffInSecs will contain the number of seconds between the date/time of execution and the the date/time value supplied in theDate. The rest is just math 🙂
Note: if you need to know whether theDate is in the future or the past, remove the math.abs() function from the 3rd line. A positive value indicates that theDate was in the past. A negative value means a future date.
Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com
You must be logged in to reply to this topic.