To format current time into yyyy-mm-dd format with JavaScript, we call toLocaleString
, toLocaleDateString
or toLocaleTimeString
.
For instance, we write
const d = new Date();
console.log(d.toLocaleString('en-SE'));
console.log(d.toLocaleDateString('en-SE'));
to create date d
with the current datetime.
And then we call toLocaleString
to return a string with the human readable locale datetime string with date and time being the values in d
.
We call toLocaleDateString
to return a string with the human readable locale date string with date and time being the values in d
.
We call them all with a locale that has dates in yyyy-mm-dd format to return a date string in this format.
Conclusion
To format current time with JavaScript, we call toLocaleString
, toLocaleDateString
or toLocaleTimeString
.