1

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

2
  • When you ask for help, please provide a context instead of how do I do A / B. Commented Oct 28, 2016 at 6:09
  • 1
    Checkout momentjs.com Commented Oct 28, 2016 at 6:14

4 Answers 4

2

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') );
  }
});
Sign up to request clarification or add additional context in comments.

5 Comments

how can it setting this in global.
use it as a directive or filter
any sample from you
This is just a formatting in view.And not creating date object
if you want to create date object from string '10-28-2016 10:27:14 AM' you need to use var dateObj = new Date('10-28-2016 10:27:14 AM');
1

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

0

You can use the default javascript method new Date("10-28-2016 10:27:14 AM") to generate it

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.