Kishan
Kishan

Reputation: 388

How to convert one date format to another format using angular JS?

In my project am getting a date like this "05/23/2016" format. But i want to show it in the view like this "23 May 2016" using Angular.

How to do it ? Please help...

Upvotes: 2

Views: 1311

Answers (4)

Nishant123
Nishant123

Reputation: 1966

First split the string "05/23/2016" on "/".

$scope.today = "05/23/2016";
$scope.date = $scope.today.split("/");

Then convert your string date into Date object

$scope.actualdate = new Date($scope.date[2],$scope.date[0] - 1,$scope.date[1]);

Now you can use any combination of the date filter from AngularJS docs

{{actualdate | date: 'dd MMMM yyyy'}} // 23 May 2016
{{actualdate | date: 'yyyy dd MMMM'}} // 2016 23 May
{{actualdate | date: 'dd MMMM'}} // 23 May

You can use any combination and format the date as you want.

EXAMPLE FIDDLE

Upvotes: 1

evsheino
evsheino

Reputation: 2287

Create a date object out of your string (in your controller, for example):

$scope.dateVar = new Date("05/23/2016");

and then use the date filter in your template:

{{ dateVar | date:'dd MMMM yyyy' }}

Upvotes: 1

Pravin Erande
Pravin Erande

Reputation: 89

Use Angular date filter

Before applying filter you date must be in Javascript Date format or in date timestamp.

Upvotes: 0

Ujjwal kaushik
Ujjwal kaushik

Reputation: 1696

If You want to use angularjs to change the format , use $filter

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

refer to this link https://docs.angularjs.org/api/ng/filter/date

Thanks

Upvotes: 0

Related Questions