-1

I have a date input field when I enter the date and do the alert in this given me 123476890017 something like this. anybody can help me on this?

If I enter the date like this 12/07/2017 and in the alert, the result should come 2017/07/12.

HTML code:

<input type="date" placeholder="search" ng-model="date">

script

 function formatDate(date) {
         alert(date);
 }
n.y
  • 3,343
  • 3
  • 35
  • 54
KK SK
  • 91
  • 1
  • 2
  • 11

3 Answers3

0

It's also very easy using a library like Moment.js:

moment("12/07/2017", "DD/MM/YYYY").format("YYYY/MM/DD");
=> "2017/07/12"
Matthew Cawley
  • 2,688
  • 1
  • 8
  • 19
0

You can try this as well:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.date = 1506413232120;
   $scope.new_date = new Date($scope.date);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
 {{new_date | date : 'yyyy/MM/dd' }}
</div>
Raghav
  • 1,139
  • 4
  • 15
  • 35
  • thank you Mr. Raghav, but i want to use ParseDate function. can you help me??? i dont know how to parse a input date string to real date. – KK SK Sep 26 '17 at 09:06
  • This may help you take a look https://stackoverflow.com/questions/4496359/how-to-parse-date-string-to-date – Raghav Sep 26 '17 at 09:44
  • @KKSK "*i want to use ParseDate function*"... well then you should specify that in your question! You're not going to get answers to do what you want, if you don't tell us what that is. – FluffyKitten Sep 26 '17 at 19:53
0

Use date filter. Click here for more details about date filter

$filter('date')(date, format, timezone)  

$scope.newDate =   $filter('date')($scope.date, "dd-MM-yyyy");

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $filter) {
  //$filter('date')(date, format, timezone) 
$scope.newDate =   $filter('date')($scope.date, "dd-MM-yyyy");

$scope.take = function(date){
$scope.newDate =   $filter('date')(date, "yyyy-MM-dd");
console.log($scope.newDate);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl"> 
  <input type="date" ng-model="date" ng-change="take(date)">
</div>
Manikandan Velayutham
  • 2,228
  • 1
  • 15
  • 22