Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

JS-JODA + Bootstrap Timepicker: "DateTimeParseException: Text could not be parsed at index 11"

While working on timezone application I mentioned in this my blogpost, I switched to JS-JODA from momentjs.com. I am quite impressed with what JS-JODA has done. I am sure you would probably be wondering why I switched from Moment JS? I couldn't just get the to initialise timezone. Reading so much articles online proves the code is broken somewhere. I tried my best seeing if I can fix it but I realised I have timeline on the project I am working on as a constraint. I have to move on. Maybe I will look into it later..Lol

My journey with JS-JODA was pretty straight forward but the biggest problem I encountered was coming by this error "DateTimeParseException: Text could not be parsed at index 11" every single time I parse the time from bootstrap timepicker to JS-JODA timezone function. After spending hours debugging and eventually giving up. I had a long sleep and woke up to look more into why this issue persists. I realise the problem is from Bootstrap timepicker 24 hours format sending hourly time in single digit. I mean 8:15 AM displayed as 8:15 instead of 08:15. Other hours with doudle digits are fine. Below was how I fixed the issue

var timeDynamic = "8:15";
timeDynamic = timeDynamic.split(":");

timeHH = ("0" + timeDynamic[0]).slice(-2);

 if (timeHH.length > 1) {

    if (timeHH >= 0 && timeHH <= 23) {

         var local = $(this).val().toString() + "T" + timeHH + ":" + timeDynamic[1];

         // copy all js-joda classes to the global scope
         var jsJoda = JSJoda;
         for (var prop in jsJoda) {
              window[prop] = jsJoda[prop];
         }

         var localTime = LocalDateTime
                            .parse(local)
                            .atZone(ZoneId.of(timezone))
                            .toString()  // 2016-06-30T11:30+02:00[Europe/Berlin]

        var zonedTime = ZonedDateTime
                            .parse(localTime)
                            .withZoneSameInstant(ZoneId.of('Asia/Kolkata'))
                            //.format(DateTimeFormatter.ofPattern('yyyy-M-d HH:mm'))
                            .format(DateTimeFormatter.ofPattern('HH:mm'))
                            .toString() // 2016-06-30T05:30-04:00[America/New_York]

        $('#time_in_india_' + index).val(zonedTime);
  }
}

I hope this helps someone. I hope this saves someone some time. Happy coding!

 


comments powered by Disqus