converting date format in angularjs controller


i write the following coding to print the current date time

$ = new Date();

and then i print the same using consol.log


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


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


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.


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

to print same on html

{{ currDate | date : "dd-MM-yyyy"}} Following formats are supported by angular.

Solution courtesy of: atulquest93


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.

$ = new Date();
The first one is :
$ =   $filter('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.


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.