Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS how to convert string "yyyyMMdd" to date

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.

like image 476
JPNN Avatar asked Dec 03 '22 18:12

JPNN


1 Answers

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>
like image 167
sylwester Avatar answered Jan 02 '23 04:01

sylwester