I'm using angular ui datepicker in my project(asp.net web api + angularjs). All works fine but when I'm trying to save date to Db it doesn't convert it to UTC format correctly and substructs 1 day(actually a few hours but it affects for a day too).
For example when I choose 01/11/2014 in the 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 like angular datepicker highlight the correct date:
I've tried to manually convert date to UTC format but unsuccessfully as 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 I specify timezone or use angular js filter?
The Javascript date can be converted to UTC by using functions present in Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string which represents the Date based on the GMT (UT) time zone.
new Date() returns relative time and not UTC time.
Java Script: var datePicker = angular. module('app', []); datePicker. directive('datepicker', function () { return { restrict: 'C', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element. datepicker({ dateFormat: 'dd, MM, yy', onSelect: function (date) { scope.
I have had the exact same problem, the solution was to remove the timezone component of the date part. To solve the problem in a more generic way I had written a 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 the timezone part and only considers the date part of the ISO date format input.
I do use moment.js for date manipulations. But you can easily convert above to not use moment.js
EDIT
use it like below
<input ng-model='abc' cs-date-to-iso ui-datepicker>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With