This topic contains 4 replies, has 2 voices, and was last updated by  Sandeep 3 years, 5 months ago.

Issue with date parse module

  • 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


    local dateparse = require ‘date.parse’
    function main()
    local T = dateparse.parse(‘2019-04-29 0615’)
    trace(‘%Y-%m-%d %H:%M’, T))

    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:

    You must be logged in to view attached files.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊

    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(‘yyyy-mm-dd HH:MM’,dateObj))
    return dateObj,dateTable

    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. ◊

    Hi Jeff,

    Thank you..:)

    Really appreciate your help.

You must be logged in to reply to this topic.