user6882159
user6882159

Reputation:

converting date format in angularjs controller

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

Upvotes: 2

Views: 41888

Answers (4)

Nitesh Mandlik
Nitesh Mandlik

Reputation: 49

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

Upvotes: 0

moks
moks

Reputation: 99

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'}}

Upvotes: 2

Atul Sharma
Atul Sharma

Reputation: 10645

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.

Upvotes: 10

Mistalis
Mistalis

Reputation: 18269

You can use the Angular date filter:

{{date | date: 'dd-MM-yyyy'}}

Upvotes: 0

Related Questions