4

can you help please to convert below in Angularjs I have value "20141023" and would like to convert to date in AngularJS and then displayed View in format dd/MMM/yyyy Many thanks N.

1
  • This is a javascript specific question, is it? Commented Oct 31, 2014 at 13:52

2 Answers 2

17

You can use regular expression please see demo below

var app = angular.module('app', []);

app.controller('homeCtrl', function($scope) {

  var st = "20130510";
  var pattern = /(\d{4})(\d{2})(\d{2})/;
  $scope.date = new Date(st.replace(pattern, '$1-$2-$3'));

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="homeCtrl">

    {{date | date :'dd/MMM/yyyy'}}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest using MomentJS (see the basic examples here) along with the AngularJS wrapper.

Using MomentJS you can define your format easily.

moment().format('YYYYMMDD')

If you want to look more into it, please refer to their documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.