1

Well, I want to show an input date field but with the format changed.

I mean, the default format is: yyyy-MM-dd (2015-11-20), but I want it: (11-20-2015).

There's a jsfiddle I found with momentjs (without angular) and works exactly as I want.

Working with AngularJs, I have done too with a directive, but with a span field (and works). But if I do with an input date, it doesn't works.

This is the html:

<body ng-app="docsTimeDirective">
  <div ng-controller="Controller">
  Current date is: <span my-current-time="format"></span><hr/>
  <input type="date" ng-model="myDate.value" my-current-time="format">
</div>
</body>

And the Js:

(function(angular) {
  'use strict';
angular.module('docsTimeDirective', [])
  .controller('Controller', ['$scope', function($scope) {
    $scope.format = 'MM/dd/yyyy';
    $scope.myDate = {
      value: new Date()
    };
  }])
  .directive('myCurrentTime', ['$interval', 'dateFilter', function($interval, dateFilter) {

    function link(scope, element, attrs) {
      var format,
          timeoutId;

      function updateTime() {
        element.text(dateFilter(new Date(), format));
        console.log(dateFilter(new Date(), format));
      }

      scope.$watch(attrs.myCurrentTime, function(value) {
        format = value;
        updateTime();
      });

      element.on('$destroy', function() {
        $interval.cancel(timeoutId);
      });

      // start the UI update process; save the timeoutId for canceling
      timeoutId = $interval(function() {
        updateTime(); // update DOM
      }, 1000);
    }

    return {
      link: link
    };
  }]);
})(window.angular);

Here is the Plunker. Some ideas?

2 Answers 2

2

This is because .text() will not apply to an <input /> element. You'll need .val() instead.

You can do a very basic check to cover both cases. Observe the following (untested)...

element[0].value !== undefined ? element.val(/*...*/) : element.text(/*...*/)

Furthermore per discussion, keep in mind the total lack of cross browser support for type="date". Perhaps finding an off the shelf datepicker could be in your best interest here - likely resolving your format restriction difficulties as well.

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

8 Comments

Ok, I tried it with element.val(dateFilter(new Date(), format)); and throws: The specified value "11/20/2015" does not conform to the required format, "yyyy-MM-dd"
with an input type="date" - it does need to be in this format. Have you confirmed the returned value of your date that dateFilter returns? Here is a fiddle that shows a date input working in its simplest form
Have you see the plunkr? In the span field returns the date as I want.
Also that first fiddle you provided - moment is doing some custom work there. If you inspect that datepicker you'll see the appropriate value as such value="2015-11-20". You'll be looking into some custom AngularJS work to get that <input> to look different
@robe007 give this fiddle a look for formatting
|
1

Well, I have solved my issue in 2 ways:

1) With MomentJs

attrs.$set('myCurrentDate', momentFilter(date, format));

and then

.filter('moment', function () {
    return function (date, formated) {
        moment.locale('es');
        var momented = moment(date).format(formated);
        return momented;
    };
});

See the Plunker


2) With Javascript native Date object

attrs.$set('myCurrentDate', dateFilter(date, format));

See the Plunker


But in both cases, with the help of css:

input:before {
    position: absolute;
    top: 3px; left: 3px;
    content: attr(my-current-date);
    display: inline-block;
    color: black;
}

PS: Note the difference between the definition of the variable $scope.format in both cases.


EDIT:

But if we don't want complications, we can use the excellent datepicker from AngularUI. It has a lot of features.

And this take me to a 3rd way for solving my issue: See the Plunker

Comments

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.