4

I'm working with Angular Material date picker. My problem is that when I send a date to the Web Api controller, I get a date less than the date I have selected in my form. I think this is due to the fact that the date value is not being localized. What I want to know is that how to localize the date in angular js

HTML:

<div ng-controller="AppCtrl" style='padding: 40px;' ng-cloak>
  <md-content>
    <h4>Standard date-picker</h4>
    <md-datepicker ng-model="myDate" md-placeholder="Enter date"></md-datepicker>
</md-content>
</div>

Controller:

angular.module('datepickerBasicUsage',
    ['ngMaterial', 'ngMessages']).controller('AppCtrl', function($scope) {
  $scope.myDate = new Date();
  $scope.minDate = new Date(
      $scope.myDate.getFullYear(),
      $scope.myDate.getMonth() - 2,
      $scope.myDate.getDate());
  $scope.maxDate = new Date(
      $scope.myDate.getFullYear(),
      $scope.myDate.getMonth() + 2,
      $scope.myDate.getDate());
  $scope.onlyWeekendsPredicate = function(date) {
    var day = date.getDay();
    return day === 0 || day === 6;
  }
});

1 Answer 1

3

To get consistent result across browsers , its best to use moment.js

Otherwise you can also use the toLocaleString() .

Lastly you can also write your own service which will get UTC time and apply necessary offsets based on the user locale.

Sign up to request clarification or add additional context in comments.

6 Comments

It would be great if you could give a demo or an example using the above code
For moment.js , the you can go through the documentation of the main site. For toLocaleString check this link :link
Would toLocalString() method be used with a $scope object?
If the date you want to get localized is : $scope.myDate , then you would use toLocaleString on it.
Also check out : material.angularjs.org/latest/api/service/$mdDateLocaleProvider. Apparently there is an angular service to handle locales.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.