bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Datepicker selects wrong date when using ISO 8601

Open john-ring opened this issue 13 years ago • 14 comments

When entering in a date in ISO 8601 format (yyyy-MM-dd), the calendar selects the entered date minus 1. So 2000-11-30 actually selects 2000-11-29.

screen shot 2013-11-20 at 11 18 02 am

john-ring avatar Nov 20 '13 16:11 john-ring

I guess that you are using AngularJS 1.0.x which had a bug where dates were parsed in the UTC time-zone instead being interpreted in the local time zone. Hard to say more without a reproduce scenario in http://plnkr.co/

pkozlowski-opensource avatar Nov 20 '13 17:11 pkozlowski-opensource

Sorry, that's a good point. I'm using AngularJS 1.0.8 because up until a few days ago it was the latest stable release. However, the screenshot above is actually from The AngularUI Bootstrap site itself - http://angular-ui.github.io/bootstrap/#/datepicker - which is also using 1.0.8, so perhaps that needs to be updated.

john-ring avatar Nov 20 '13 17:11 john-ring

Still have this issue with angular 1.2 and angular-bootstrap 0.10.0. I select 3rd of February and the model contains 4th and so forth. And I can only reproduce if I set San Francisco (Pacific CA-USA) time, not Europe time.

dragosrususv avatar Jan 28 '14 13:01 dragosrususv

I'm not sure if this issue is related with angular or bootstrap, but I solved just using the same format date in both sides.

Server:

def as_json(options = {})
  {
    id: model.id,
    date: model.date.strftime("%d-%m-%Y")
  }
end

Client

<input type="text" datepicker-popup="dd-MM-yyyy" ng-model="model.date" class="form-control" datepicker-options="dateOptions" show-weeks='false' is-open="opened" min="minDate" max="'2015-06-22'"  date-disabled="disabled(date, mode)" close-text="Close" />

Regards!

joselo avatar Feb 14 '14 05:02 joselo

Have the same problem when the date is set to dd.MM.yyyy. Angularjs version is 1.2 and angular-ui is version 0.10. This only happens when I enter the date manually.

zszep avatar Feb 28 '14 10:02 zszep

Maybe this has something to do with the timezone I'm in?

zszep avatar Feb 28 '14 10:02 zszep

We solved this problem by cutting off timezone information when we provide the date to the date picker. Example:

  • server sends 28th of February 2014 15:05 GMT+2
  • client is on PST timezone
  • on client side we parse this date and instantiante: new Date(2014, 1, 28) // mind the "1" as February, months start from 0. => this basically generates: 28th of February 2014 00:00 PST and provides this to the date picker - some sort of "local equivalent"

Alternatively you could use moment.js to convert to this "local equivalent" if you don't want to do'it manually.

dragosrususv avatar Feb 28 '14 10:02 dragosrususv

@dragosrususv can you provide some more detail in your fix? Where in the code did you cut off the timezone data?

thoughtpunch avatar Mar 17 '14 14:03 thoughtpunch

On 17.03.2014 15:49, Dan Barrett wrote:

@dragosrususv https://github.com/dragosrususv can you provide some more detail in your fix? Where in the code did you cut off the timezone data?


Reply to this email directly or view it on GitHub https://github.com/angular-ui/bootstrap/issues/1278#issuecomment-37823776.

I fixed it eventually on the server side. I am using ASP.Net Web Api for the server side code. There, upon receiving the json date, I simply convert it to local time: var newValue = ((DateTime)value).ToLocalTime();

zszep avatar Mar 18 '14 06:03 zszep

@thoughtpunch : There are 2 options:

  1. you cut-off time* information on server side and UI receives "2014-03-18" - instantiate a date object like "new Date(2014, 2, 18)" will get you the proper input for the date picker component
  2. you cut-off time* information on client side; UI extracts year, month and day from that and instantiates a date object as the above

In any case, you can either do your own dateService to handle this, either use moment.js for the "local equivalent".

Hope it helps.

dragosrususv avatar Mar 18 '14 10:03 dragosrususv

I was able to solve the problem by adding a Json Converter with the format yyyy-MM-ddTHH:mmZ as per the angular date filter

MVC WebApi:

static public void RegisterJsonConverters(HttpConfiguration config) { config.Formatters.JsonFormatter.SerializerSettings.Converters.Add( new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mmZ" }); }

Works for version 011.0

njsent avatar Jul 06 '14 06:07 njsent

see my comment at: https://github.com/eternicode/bootstrap-datepicker/issues/308

Paki90 avatar Jul 13 '14 21:07 Paki90

my solution is to use a directive to fix the minus 1 issue, and also to format the view and model values:

app.directive('ngBootstrapFix',['$filter', function($filter) { return { require: 'ngModel', priority: 1, link: function($scope, $element, $attrs, ngModelCtrl) { ngModelCtrl.$parsers.push(function(viewValue) { viewValue = $filter('date')(viewValue, 'yyyy-MM-dd'); return viewValue; }); ngModelCtrl.$render = function() { var val = $filter('date')(ngModelCtrl.$viewValue, 'dd/MM/yyyy'); $element.val(val); }; } }; }]);

admhemed avatar Feb 01 '15 21:02 admhemed

Fantastic! Thanks for the directive, this is perfect! I added one comment to my code: Usage: <input ng-bootstrap-fix="currentResource.date_validated" ...

And modified the $render $filter: var val = $filter('date')(ngModelCtrl.$viewValue, 'MMM dd, yyyy');

teracomp avatar Feb 09 '15 17:02 teracomp