1

Trying to display just the date by doing this:

let myDate="2015-03-08T04:49:49.431Z";
console.log(new Date(myDate).toUTCString('dd-mm-yyyy'));

Still returns the time , how can I just return the date?

bier hier
  • 20,970
  • 42
  • 97
  • 166
  • Did you try https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript – weirdpanda Nov 25 '17 at 23:50
  • Possible duplicate of [How do I get the current date in JavaScript?](https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript) – weirdpanda Nov 25 '17 at 23:50

2 Answers2

2
var myDate="2015-03-08T04:49:49.431Z";
new Date(myDate).toLocaleDateString(); // "3/8/2015"
new Date(myDate).toDateString(); // "Sun Mar 08 2015"

You can check all other methods here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Bikas
  • 856
  • 7
  • 16
0

You could do something like:

let myDate = "2015-03-08T04:49:49.431Z";
let date = new Date(myDate);
let dateString = ("0" + date.getUTCDate()).slice(-2) + "-" + ("0" + (date.getUTCMonth()+1)).slice(-2) + "-" + date.getUTCFullYear();
console.log(dateString);

.toUTCString() does not accept any arguments.

dferenc
  • 7,918
  • 12
  • 41
  • 49