Angular UI datepicker giving me a UTC converted string. How to parse it back to local time accounting for the time difference?

StackOverflow https://stackoverflow.com/questions/22548306

Question

I am currently running into issues with datepicker automatically converting my time to UTC. Is there anything I can pass into datepicker for it to not give me back a UTC converted string? For example the user picks March 19 on the calendar and the returned string would be something like this

'2014-03-19T04:00:00.000Z'

What I want is ->

'2014-03-19T00:00:00-04:00'

What I am trying now (sort of hacking around it) is trying to use moment js to convert it back to my desired (expected) format, but I am having trouble doing so without hardcoding a subtraction in there. I want to be able to convert it from UTC back to local time.

Does anyone know of a solution to this using moment js or angular?

Was it helpful?

Solution

I ran into the same problem and used a filter that adjusts the date and time using the local data to get around it:

filter('adjustDatepicker', ['$filter', function($filter){
    var dateFilter = $filter('date');
    return function(dateToFix){
        var localDate, localTime, localOffset, adjustedDate;

        localDate       = new Date(dateToFix);
        localTime       = localDate.getTime();
        localOffset     = localDate.getTimezoneOffset() * 60000;
        adjustedDate    = new Date(localTime + localOffset);

        return dateFilter(adjustedDate, 'MM/dd/yyyy');
    };
}])

Use it like this in your template file:

{{details.datetomodify | adjustDatepicker}}

OTHER TIPS

I think new Date('2014-03-19T04:00:00.000Z').toString() will give you the local version of the UTC time.

time

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top