Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular $http.post changing date to UTC date

I was trying to post some data to my REST api which has date. Now while I debug, my date parameter is a JS Date object with correct date in my timezone: Tue Apr 04 2017 00:00:00 GMT+0530

after it leaves my code, and I see the same in network tab, it is converted to UTC date: "2017-04-03T18:30:00.000Z"

I searched for the solution according to which I need to include locale file of angular in my index.html which I did:

<script type="text/javascript" src="resources/js/angular/angular-locale_en-in.js"></script> 

but it doesn't help. I've seen solutions like adding date format to filter or something, but I want a global solution. Any help? Thanks :)

like image 471
pranavjindal999 Avatar asked Apr 19 '17 13:04

pranavjindal999


People also ask

How do you convert Date to UTC Date?

To convert a JavaScript date object to a UTC string, you can use the toUTCString() method of the Date object. The toUTCString() method converts a date to a string, using the universal time zone. Alternatively, you could also use the Date. UTC() method to create a new Date object directly in UTC time zone.

What is UTC string?

Definition and Usage. The toUTCString() method returns a date object as a string, according to UTC. Tip: The Universal Coordinated Time (UTC) is the time set by the World Time Standard. Note: UTC time is the same as GMT time.


1 Answers

Handling date, time, and timezone have confused me too. May be this answer gives you some insight on how you can handle them.

Try the following code in Chrome's developer console and see how same date is presented in different formats:

var date = new Date(); date.toISOString(); // "2017-04-29T09:54:28.714Z" date.toGMTString(); //"Sat, 29 Apr 2017 09:54:28 GMT" date.toLocalString(); //"4/29/2017, 3:24:28 PM" 

Any date that you create on client always records the date at zero timezone offset i.e. UTC+/-00:00 Z. For simplicity you may think UTC and GMT as same. When it comes to display purpose the same date is presented as per the browser's timezone. If you do console.log (date) it'll output Sat Apr 29 2017 15:24:28 GMT+0530 (IST) but that doesn't mean that the internal recording of the date is as per browser's timezone. It's just presented on screen/console as per browser's timezone.

Look at date representations not as being converted from one timezone to another but look at them as different representation of the same date. In your browser it is represented as GMT+0530 offset and when it is sent to server it is the same date at zero timezone offset.

As per your comment, if you choose 4th Apr at 00:00 AM in GMT+0530 timezone, internally it'll be 3rd Apr at 18:30 PM in at GMT+0 i.e. zero timezone offset. Let it go to server as it is. When you need to use this date, it comes back from server as 3rd Apr and it'll be displayed in browser as per the browser's timezone offset. There is no conversion involved, it is one date with different representation.

I once asked a related question, may be this adds more clarification.

And overall, this answer is still same as @geminiousgoel and @charlietfl answers.

like image 83
Ritesh Jagga Avatar answered Oct 14 '22 07:10

Ritesh Jagga