Reputation: 25
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
Upvotes: 1
Views: 11770
Reputation: 169
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.
Upvotes: 0
Reputation: 1260
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') );
}
});
Upvotes: 2
Reputation: 17956
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())
Upvotes: 1
Reputation: 19986
You can use the default javascript
method new Date("10-28-2016 10:27:14 AM")
to generate it
Upvotes: 0