0

I have an object that has a date field that is "2015-01-31T00:00:00.000Z" I want to display 1/31/15 in my app. The problem is that it displays my date in my time zone, as opposed to UTC, so it actually shows 1/30/15 instead.

I've looked all around and can't find a clean solution to simply display the date without bringing in Angular 1.3 or Moment.

KJ3
  • 5,168
  • 4
  • 33
  • 53

1 Answers1

0

I decided to create my own filter with a bit of help from the SO post.

.filter('utcDate', function ($filter) {
    return function (theDate) {
        var dateFilter = $filter('date');

        originalDate = new Date(theDate);

        currentDate = new Date();
        utcDate = millisToUTCDate(currentDate);

        millisecondsToUtc = utcDate - currentDate;

        originalDate.setHours(originalDate.getHours() + toHours(millisecondsToUtc))

        return dateFilter(originalDate, 'shortDate');
    };
});

function toUTCDate(date) {
    var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
return _utc;
};

function millisToUTCDate(millis) {
    return toUTCDate(new Date(millis));
};

function toHours(milliseconds) {
    return Math.round(milliseconds / 1000 / 60/ 60)
} 
Community
  • 1
  • 1
KJ3
  • 5,168
  • 4
  • 33
  • 53