AngularJS how to convert the string "yyyyMMdd" today

can you help convert below to Angularjs I have a value of "20141023" and would like to convert to AngularJS today and then display the View in dd / MMM / yyyy format Thank you very much N.

+4
source share
2 answers

You can use regex 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>
Run codeHide result
+16
source

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

Using MomentJS, you can easily define your format.

moment().format('YYYYMMDD')

, .

+2

All Articles