0

I need to show date in the same timezone as it coming in from service. But angular date filter converts the date to local timezone. is there a way to avoid conversion in local timezone. My input is 2014-12-11T05:21:22.323-05:00 required output - 12/11/2014 05:21:22 PM (without converting to local timezone)

Thanks in advance.

2
  • possible duplicate: stackoverflow.com/questions/21133776/ignore-time-zone-angularjs Commented Dec 11, 2014 at 11:32
  • Thanks Evandro for quick comment. The answer provided in that question shows date time in utc format. I want to preserve original timezone. Please correct if I am missing something. Commented Dec 11, 2014 at 11:40

3 Answers 3

1

You can create a custom filter for this, just convert the desired date without the timezone, for example:

myApp.filter('ignoreTimeZone', function(){
  return function(val) {
      var newDate = new Date(val.replace('T', ' ').slice(0, -6));
     return newDate;
  };
});

Check this fiddle: http://jsfiddle.net/ghxd6nom/

You can also use a library for this, like moment.js

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Evandro. It is working in chrome. But it is not working in firefox.
0

Angular provides service '$filter'. We can inject this service on controller.

with the help of this service, we can format the date .

var dateFrom = $filter('date')(new Date(), "MM/dd/yyyy");

in this way, we can able to format the date with or without Timezone.

The return valus is string and we can convert the string into DateTime on Backend.

The otherway is to use library moment.js. This library provides better way of converting/formatting Dates.

check this blog , which provides angular date/time filtering & formatting.

Comments

0

You can use this :

$filter('date')(date, format, '+0000')

Documentation: https://docs.angularjs.org/api/ng/filter/date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.