nb-sdouglas

lr-notebook/


Fix datepicker local time

2016-05-15
Attachments:


Problem

lr-notebook uses the JavaScript Date() function to pre-populate "today" on various date pickers. It displays the current date for GMT, but we want the current date for local time.


Displays 5/16 late on 5/15.

Solution

This is a common problem, just need to update our function with an existing solution found online.

http://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset

Old

(function () {
    var date = new Date().toISOString().substring(0, 10),
        field = document.querySelector('#date');
    field.value = date;
})()

New

(function () {
    var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
    var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1).substring(0, 10);
    document.querySelector('#date').value = localISOTime;
})()