Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting date format in angularjs controller

Tags:

angularjs

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


2 Answers

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.

like image 132
Atul Sharma Avatar answered Feb 06 '26 00:02

Atul Sharma


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'}}
like image 32
moks Avatar answered Feb 06 '26 01:02

moks