0

I would like to know how to get HTML 5 date input value get to script. This date format should be in YYYY-MM-DD. This value should be store in date1 variable after click the submit button.

HTML Code:

    <form ng-controller="validateCtrl" name="myaForm" novalidate> 
    <input class="datepicker validate" type="date" id="birthday" ng-model="birthday" placeholder="yyyy-MM-dd" required>
    <button type="submit" ng-submit="submitfun()">Submit</button>
    </form>

Script Code:

<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.submitfun= function () {
birthday = $filter('date')(birthday, 'yyyy/MM/dd'); 
}
});
</script>

This code not working for me how to solve this problem.

user43153
  • 139
  • 6

2 Answers2

0

You missed to declare scope variable. Write $scope.birthday.

Manikandan Velayutham
  • 2,228
  • 1
  • 15
  • 22
0

There is no way possible to change the format of html5 input date type, look at possible solution in the below answer:

How can I load value on input date with angular?

I'll add to that if you would like to get the formatted string value of the date, then you either use $moment library like this:

<form ng-controller="validateCtrl" name="myaForm" novalidate> 
  <input class="datepicker validate" type="date" id="birthday" ng-model="birthdayModel" placeholder="yyyy-MM-dd" required>
  <button type="submit" ng-submit="submitfun()">Submit</button>
</form>


$scope.submitfun= function () {
  $scope.birthday = $moment($scope.birthdayModel).format('YYYY-MM-DD');
  console.log($scope.birthday.toString());
}

or parse the date manually like this:

$scope.submitfun= function () {
  var days = $scope.birthdayModel.getDate();
  var months = $scope.birthdayModel.getMonth()+1;
  var years = $scope.birthdayModel.getFullYear();
  //Now you have them separate you can put them in whatever format you want 
  //like this for example:
  $scope.birthday = days+'-'+months +'-'+years ;
  console.log($scope.birthday);
}
Mostafa Omar
  • 167
  • 2
  • 6