1

Consider:

var myDate = $scope.today;
var previousDay = new Date(myDate);
previousDay.setDate(myDate.getDate() - 1);
var date = previousDay; // 2017-03-04 09:46:22.103 +5.30 GMT

How do I get to only have "2017-03-03 09:46:22.103"?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jayesh patil
  • 63
  • 12
  • 1
    AFAIK Angular doesn't have anything that allows you to format a date. You will probably want to use vanilla JS instead. See http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date. – Samir Aguiar Mar 04 '17 at 04:57
  • Use this for further reference http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – sgsvnk Mar 04 '17 at 05:06

2 Answers2

1

You can use

var requiredDateFormat = previousDay.toLocaleDateString() + " " + previousDay.toLocaleTimeString();
sgsvnk
  • 1,991
  • 2
  • 17
  • 45
  • The format of *toLocaleDateString* is entirely implementation dependent and varies from host to host. There is no guarantee that the result will have the format in the OP. For me, Safari gives "3/5/2017", Firefox "05/03/2017". – RobG Mar 04 '17 at 21:03
0

You can use new Date().toJSON();. It will return "2017-03-04T04:55:50.786Z".

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
raja reddy
  • 339
  • 2
  • 9