1

What I want to do is print date in below format in angularjs.

Aug 30, 2016
{{CC.StartDate | date }} //angularjs UI code

I am just sending the modal with response, which will automatically deserialize object into json.

List<CClass> Items = CClass.GetMultipleAsObject(); //fetch data   
return Request.CreateResponse(Items); //sending it to UI

I can see that I am receiving "2016-08-30T00:00:00" in response in network tab. But when it gets rendered in different browser, it renders it differently.

I am in IST timezone, Chrome render it as "Aug 30, 2016". But IE and FF render it as "Aug 29, 2016". In PST timezone all browser render it correct. Date looks like in UTC.

I tried converting it into a plain string, still same issue. Tried this - $filter('date')(input, "yyyy-MM-dd");, still same issue

I haven't tried momentjs, thought it must be possible without that also.

Can someone guide, how I can I get rid of this timezone localization and just show date in string ire-respective of timezone?

Pengyy
  • 37,383
  • 15
  • 83
  • 73
sunder
  • 1,803
  • 4
  • 29
  • 50
  • By using angularjs filter also it will convert with timezone. I don't think problem is with browser.. bcz I am just converting "2016-08-30T00:00:00" in IE it is showing 30 Aug 2016 only. you need solution in javascript... this might be helpful. http://stackoverflow.com/questions/2771609/how-to-ignore-users-time-zone-and-force-date-use-specific-time-zone – Nitish Apr 17 '17 at 05:53
  • i just checked with your format i didn't found anything like mentioned.... – Sa E Chowdary Apr 17 '17 at 06:15
  • I tried same @Nitish and it works, but when I render via Json object it goes 1 day back. As I mentioned I tried different solution but its not working. – sunder Apr 17 '17 at 06:23
  • You are trying in string, if its from json object it goes back a day as per localized timezone. https://nulogy.com/who-we-are/company-blog/articles/dealing-with-timezones-in-javascript/ – sunder Apr 17 '17 at 06:25

1 Answers1

0

Date is always tricky with javascript. In your scenario you need to display the date without involving timezone. You can use getUTCDate() getUTCMonth() and getUTCFullYear() to get the individual elements from your date string and construct a new date object.

Following example returns the same date in both ie and chrome.

JSFiddle link

Hope this helps.

Gowthaman
  • 1,262
  • 1
  • 9
  • 15