Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
xoceunder
Roku Guru

Change date to Unix Timestamp

How do I change this date MON, MAY 29 2017 2:05 pm with roDateTime to asSeconds
0 Kudos
4 REPLIES 4
belltown
Roku Guru

Re: Change date to Unix Timestamp


' Convert date from this format: "MON, MAY 29 2017 2:05 pm" to seconds '
function dateStrToSecs(dateStr) as Integer

   months = {
       jan: "01",
       feb: "02",
       mar: "03",
       apr: "04",
       may: "05",
       jun: "06",
       jul: "07",
       aug: "08",
       sep: "09",
       oct: "10",
       nov: "11",
       dec: "12"
   }

   re = CreateObject("roRegex", "([A-Z]+)\s*(\d{1,2})\s*(\d{4})\s*(\d{1,2}):(\d{2})\s*(am|pm)", "i")

   ma = re.Match(dateStr)

   if ma.Count() <> 7
       print "Invalid date format"
       return 0
   end if

   yyyy  = ma[3]
   mon   = months[ma[1]]
   dd    = Right("0" + ma[2], 2)
   hhInt = ma[4].ToInt()
   mm    = ma[5]
   ampm  = ma[6]

   if LCase(ampm) = "pm" and hhInt < 12 then hhInt = hhInt + 12
   hh = Right("0" + hhInt.ToStr(), 2)

   isoStr = yyyy + "-" + mon + "-" + dd + " " + hh + ":" + mm + ":00"
   print isoStr

   dt = CreateObject("roDateTime")
   dt.FromISO8601String(isoStr)

   return dt.AsSeconds()

end function
0 Kudos
xoceunder
Roku Guru

Re: Change date to Unix Timestamp

"belltown" wrote:

' Convert date from this format: "MON, MAY 29 2017 2:05 pm" to seconds '
function dateStrToSecs(dateStr) as Integer

   months = {
       jan: "01",
       feb: "02",
       mar: "03",
       apr: "04",
       may: "05",
       jun: "06",
       jul: "07",
       aug: "08",
       sep: "09",
       oct: "10",
       nov: "11",
       dec: "12"
   }

   re = CreateObject("roRegex", "([A-Z]+)\s*(\d{1,2})\s*(\d{4})\s*(\d{1,2}):(\d{2})\s*(am|pm)", "i")

   ma = re.Match(dateStr)

   if ma.Count() <> 7
       print "Invalid date format"
       return 0
   end if

   yyyy  = ma[3]
   mon   = months[ma[1]]
   dd    = Right("0" + ma[2], 2)
   hhInt = ma[4].ToInt()
   mm    = ma[5]
   ampm  = ma[6]

   if LCase(ampm) = "pm" and hhInt < 12 then hhInt = hhInt + 12
   hh = Right("0" + hhInt.ToStr(), 2)

   isoStr = yyyy + "-" + mon + "-" + dd + " " + hh + ":" + mm + ":00"
   print isoStr

   dt = CreateObject("roDateTime")
   dt.FromISO8601String(isoStr)

   return dt.AsSeconds()

end function


Thanks for your help but does not match the date
0 Kudos
belltown
Roku Guru

Re: Change date to Unix Timestamp

"xoceunder" wrote:
Thanks for your help but does not match the date

I'm not sure what you mean by "does not match the date".

One possibility is that your roDateTime object needs to be in the local time zone. If this is the case then after this code:

   dt = CreateObject("roDateTime")
   dt.FromISO8601String(isoStr)

add this line:

   dt.ToLocalTime()
0 Kudos
xoceunder
Roku Guru

Re: Change date to Unix Timestamp

"belltown" wrote:
"xoceunder" wrote:
Thanks for your help but does not match the date

I'm not sure what you mean by "does not match the date".

One possibility is that your roDateTime object needs to be in the local time zone. If this is the case then after this code:

   dt = CreateObject("roDateTime")
   dt.FromISO8601String(isoStr)

add this line:

   dt.ToLocalTime()


Adding this dt.ToLocalTime () adds me 4 hr and without this dt.ToLocalTime () takes me 4 hr  :shock:
0 Kudos