2

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

  • 1
    Take a look to the [Angular date filter](https://docs.angularjs.org/api/ng/filter/date). – Mistalis Jan 24 '17 at 11:13
  • Possible duplicate of [How to format date in angularjs](http://stackoverflow.com/questions/22392328/how-to-format-date-in-angularjs) – Ankh Jan 24 '17 at 11:22

4 Answers4

10

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.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
2

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'}}
moks
  • 99
  • 5
0

You can use the Angular date filter:

{{date | date: 'dd-MM-yyyy'}}
Mistalis
  • 17,793
  • 13
  • 73
  • 97
0

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

S.R
  • 2,819
  • 2
  • 22
  • 49