angularjs - javascript does't convert angular ui datepicker date to UTC correctly -
i'm using angular ui datepicker in project(asp.net web api + angularjs). works fine when i'm trying save date db doesn't convert utc format correctly , substructs 1 day(actually few hours affects day too).
for example when choose 01/11/2014 in datepicker:
angularjs object: sat nov 01 2014 00:00:00 gmt+0200 (fle standard time) in request: 2014-10-31t22:00:00.000z in asp.net api controller: {31-oct-14 10:00:00 pm}
datepicker controller:
app.controller('datepickerctrl', function ($scope) { $scope.today = function () { $scope.dt = new date(); }; $scope.today(); $scope.clear = function () { $scope.dt = null; }; $scope.open = function ($event) { $event.preventdefault(); $event.stoppropagation(); $scope.opened = true; }; $scope.format = 'dd/mm/yyyy'; $scope.dateoptions = { 'starting-day': 1 }; });
hnml:
<div class="form-group inputgroupcontainer" ng-controller="datepickerctrl"> <label for="date">date of birth</label> <p class="input-group"> <input type="text" name="date1" ui-date="{dateformat: 'yy-mm-dd'}" ui-date-format="yy-mm-dd" placeholder="dd/mm/yyyy" class="form-control" datepicker-popup="{{format}}" ng-model="user.personal.birthdate" max-date="currentdate" is-open="opened" close-text="close" /> <span class="input-group-btn"> <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button> </span> </p> </div>
but seems angular datepicker highlight correct date:
i've tried manually convert date utc format unsuccessfully well
toutcdate: function (d) { var date = new date(d); var _utc = new date(date.getutcfullyear(), date.getutcmonth(), date.getutcdate());//, date.getutchours(), date.getutcminutes(), date.getutcseconds() return _utc; }
should specify timezone or use angular js filter? i'll appreciate help!
i have had exact same problem, solution remove timezone component of date part. solve problem in more generic way had written directive
csapp.directive("csdatetoiso", function () { var linkfunction = function (scope, element, attrs, ngmodelctrl) { ngmodelctrl.$parsers.push(function (datepickervalue) { return moment(datepickervalue).format("yyyy-mm-dd"); }); }; return { restrict: "a", require: "ngmodel", link: linkfunction }; });
above directive removes timezone part , considers date part of iso date format input.
i use moment.js date manipulations. can convert above not use moment.js
edit
use below
<input ng-model='abc' cs-date-to-iso ui-datepicker>
Comments
Post a Comment