converting date format in angularjs controller

Problem

i write the following coding to print the current date time

$scope.date = new Date();

and then i print the same using consol.log

console.log($scope.date);

and it is working fine

Tue Jan 24 2017 16:36:06 GMT+0530 (India Standard Time)

but now i want to change the date format and i want to print like

21-12-2016

can anybody help me here?

i used the conversion but i am unable to remember the page or the url of the page right now,

and stuck on this,

before i leave for the home today i thought of solving this issue

Problem courtesy of: Information HTI

Solution

In controller you can do

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

to change the date format. And in html,

{{ date_expression | date : format : timezone}}

use this.

Like

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

to print same on html

{{ currDate | date : "dd-MM-yyyy"}}

https://docs.angularjs.org/api/ng/filter/date Following formats are supported by angular.

Solution courtesy of: atulquest93

Discussion

You can use the Angular date filter:

{{date | date: 'dd-MM-yyyy'}}
Discussion courtesy of: Mistalis

You can do this either in controller or in html page.

$scope.date = new Date();
The first one is :
$scope.date =   $filter('date')($scope.date, 'dd-MM-yyyy');

Second one is : 
{{date | date:'dd-MM-yyyy'}}
Discussion courtesy of: moks

You can use the in-build js libraries functions i.e getDay(), getHours(), getMinutes(), getMilliseconds(). This functions will return you the corresponding date's individual components values.

e.g

var x = $scope.yourDateModelObj.getHours();

Likewise, you can get the date, month, years values.

return an integer value for hours.

Hope that helps

Discussion courtesy of: Nitesh Mandlik

This recipe can be found in it's original form on Stack Over Flow.