简体   繁体   中英

Date Time Format AngularJs

I have a string output as "30/12/2019 12:00:00 AM" ie "DD/MM/YYYY HH:MM:SS AM". I need a formatter to display date "30/12/2019 12:00:00 AM" as Dec 30 2019. Can anyone help me on this. Thanks.

The date string you want to format should be either a Date object, milliseconds (string or number) or various ISO 8601 datetime string formats.

You cannot create a date in DD/MM/YYYY HH:MM:SS AM format using Date object in JavaScript.

Here is a simple workaround that converts the date string into a valid date object and then formats date in specified format using AngularJS date filter .

 const app = angular.module('myApp', []); app.controller('myController', ['$scope', function($scope){ const convertDate = date => { const parts = date.split('/'), day = parts[0], month = parts[1], year = parts[2].split(' ')[0]; return new Date(`${year}-${month}-${day}`); }; $scope.dt = convertDate('30/12/2019 12:00:00 AM'); }]);
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script> <div ng-app="myApp" ng-controller="myController"> {{ dt | date: 'mediumDate' }} </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM