Reputation: 42957
I am not so into JavaScript. I have the following problem trying to format a Date
. I can use only plain old JavaScript (no third party library because this script is not performed into the browser but into a Java application using Rhino).
I created my Date
object in this way:
d = new Date('2017','11','09','06','00','00');
(into the Rhino environment the Date()
constructor works only in this way).
That creates a Date
object like this:
Sat Dec 09 2017 06:00:00 GMT+0100 (CET)
Starting from this Date
object I want to obtain a String formatted in this way:
yyyy-mm-dd HH:mm:ss
I know that doing:
d.toISOString()
I obtain
2017-12-09T05:00:00.000Z
but it contains the T and Z delimiters.
What is a smart way to do it?
Upvotes: 1
Views: 1444
Reputation: 191946
Reformat the ISO string by replace the unnecessary parts with a space, then String#trim the spaces from the end.
Thanks to @zerkms for the regex expression.
var d = new Date('2017','11','09','06','00','00');
var str = d.toISOString().replace(/T|Z|\.\d{3}/g, ' ').trim();
console.log(str);
Upvotes: 9