Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 504
JaTo Avatar asked Mar 21 '14 01:03

JaTo


People also ask

How to display UTC date and time in angular using date pipe?

How to display UTC date time in angular using date pipe. To display UTC date and time in Angular we have to pass timezone parameters as ‘UTC’ or timezone offset as ‘+0000’ as shown below. Today is { {todayDate | date:'short':'UTC'}} Today is { {todayDate | date:'short':'+0000'}} Result: Today is 6/19/19, 11:11 AM.

How to use date&time picker in angular forms?

Run the following command to install the @angular-material-components/datetime-picker in the Angular project For using the Datepicker with Time selection, we need to import Material’s default MatDatepickerModule and MatInputModule modules. Other than these also import the FormsModule and ReactiveFormsModule to use the Date & Time picker in forms.

How to change the datetime format in angular using MMM?

All types of datetime values displays the date in ‘MMM d, y’ format which is default Angular date format ‘mediumDate’. To change the datetime format in angular we have to pass date time format parameter to the angular pipe as shown below. { { date_value | date :'short'}} // 6/15/19, 5:24 PM.

How to display date according to country locale format in angular?

To display date according to country locale format rules, We have to pass country locale code as a third parameter to angular date pipe as shown below. For example France follows Central European Summer Time and it has an timezone offset ‘+0200’.


1 Answers

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}}
like image 135
rg88 Avatar answered Oct 12 '22 04:10

rg88