How to send AngularStrap datepicker value without timezone? How to send AngularStrap datepicker value without timezone? angularjs angularjs

How to send AngularStrap datepicker value without timezone?


The issue isn't AngularStrap. Its just how javascript dates work and how JSON formats them for transmission. When you turn a javascript date object into a JSON string, it formats the string as UTC.

For example, I'm in Utah and it is now 07:41 on 2013-10-24. If I create a new javascript date and print it to the console it will say:

Thu Oct 24 2013 07:41:19 GMT-0600 (MDT)

If I stringify that same date (using JSON.stringify(date), I get:

"2013-10-24T13:41:47.656Z"

which you can see is not in my current timezone, but is in UTC. So the conversion is happening just before the form gets sent to the server when it gets converted from a javascript object to a JSON string.

The easiest way to do it would be to just change the date to a string of your own choosing prior to sending the date to the server. So instead of letting JSON change the date to UTC, (assuming you don't care about the time of day) you could just do something like this:

var dateStrToSend = $scope.date.getUTCFullYear() + '-' + ($scope.date.getUTCMonth() + 1) +  '-' + $scope.date.getUTCDate();

That will give you a UTC-based string that looks like '2013-10-24' and then you can send that to the server, instead of the JSON format which includes the time info. Hopefully that helps.

UPDATE: As @Matt Johnson said, there are two ways to do it. You said: How could we prevent the conversion, or use UTC dates during the whole process?. If you want to use UTC, then use my above explanation. If you want to just "prevent the conversion", you could use the following:

var dateStrToSend = $scope.date.getFullYear() + '-' + ($scope.date.getMonth() + 1) +  '-' + $scope.date.getDate();


A bit late but I spent my afternoon on this and someone might find it useful.

Another way to do this declaratively is to use the dateType, dateFormat and modelDateFormat attributes. Set these in either the config or the HTML e.g

angular.module('app').config(function ($datepickerProvider) {    angular.extend($datepickerProvider.defaults, {        dateFormat: 'dd-MMMM-yyyy',        modelDateFormat: "yyyy-MM-ddTHH:mm:ss",        dateType: "string"    });});

DateFormat is the format the date will be displayed to the user in the date picker while modelDateFormat is the format it will be converted to before being bound to your model.

I also had default values coming from the server which I needed to be bound to the datepicker on page load. I therefore had to update the format the server serialized dates in JSON to match the modelDateFormat. I am using Web API so I used the below.

var jsonSettings = Formatters.JsonFormatter.SerializerSettings;jsonSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss";


The "Angular way" is to use the $filter service to format the date returned by the datepicker.

Example (HTML):

{{inpDate | date: 'dd-MM-yyyy'}}

Example (JS):

$scope.processDate = function(dt) {return $filter('date')(dt, 'dd-MM-yyyy');}

Plunker here