-1

Doing new Date().toString() generates this : Fri May 14 2021 10:16:12 GMT+0200 (Central European Summer Time)

But i am looking for something like : 2021-05-14 08:08:11.

I have no clue how to get this format and hope someone could help me out here.

kevin
  • 1
  • 1

3 Answers3

1

Try using a function like this

function formatDate(value) {
    let date = new Date(value);
    const day = date.toLocaleString('default', { day: '2-digit' });
    const month = date.toLocaleString('default', { month: 'short' });
    const year = date.toLocaleString('default', { year: 'numeric' });
    return day + '-' + month + '-' + year;
}

console.log(formatDate(new Date));
0

You can try this if you want.

The gist: You will be needing to create a format on your own.

let today = new Date();

let final_date = "";

final_date+=today.getFullYear();

final_date+="-";

final_date+=today.getMonth();

final_date+=today.getDate();

final_date+=" ";

final_date+=today.getHours();

final_date+=":";

final_date+=today.getMinutes();

final_date+=":";

final_date+=today.getSeconds();

console.log(final_date);

0

Please try this code. Good Luck!

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var m = new Date();
var dateString = m.getUTCFullYear() + "-" +
    ("0" + (m.getUTCMonth()+1)).slice(-2) + "-" +
    ("0" + m.getUTCDate()).slice(-2) + " " +
    ("0" + m.getUTCHours()).slice(-2) + ":" +
    ("0" + m.getUTCMinutes()).slice(-2) + ":" +
    ("0" + m.getUTCSeconds()).slice(-2);
document.getElementById("demo").innerHTML = dateString;
</script>
</body>
</html>
Nikita
  • 964
  • 2
  • 14