I need to convert "10-28-2016 10:27:14 AM" string datetime to datetime Fri Oct 28 2016 12:04:16 in angularJs Pls Help
4 Answers
view
<p>{{'10-28-2016 10:27:14 AM' | filter}}</p>
filter and controller
angular
.module('myApp',[])
.run(function($rootScope){
$rootScope.title = 'myTest Page';
})
.controller('testController', ['$scope', function($scope){
}]).filter('filter', function($filter) {
return function(input) {
var date = new Date(input);
return($filter('date')(date, 'EEE MMM dd yyyy HH:mm:ss') );
}
});
5 Comments
'10-28-2016 10:27:14 AM' you need to use var dateObj = new Date('10-28-2016 10:27:14 AM');Best bet is to read through MDN Date documentation. Here is an example:
const date = new Date('10-28-2016 10:27:14 AM')
console.info(date.toDateString())
console.info(date.toTimeString())
console.info(date.toUTCString())
Comments
The best way should be to convert string-date to Date object, using moment.js or natively, where string first occurs (you are either getting it via API or from user's input). http://momentjs.com/docs/#/parsing/string-format/
And then just use angular's built-in date filter to display formatted value as you like, for example:
{{ date_expression | date : format : timezone}}
https://docs.angularjs.org/api/ng/filter/date
When you are ready to send date back to the server, you should convert it to UTC string and persist it like that in the database, to avoid all the time zones issues.
date.toUTCString();
Where date is valid Date object, previously stored as such.