Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format date without time using toLocaleTimeString

I have a Date, which I want to convert to a string, but without the time, just the date part.

My current code:

var date = new Date(2014, 9, 08); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time) 
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};

console.log(date.toLocaleTimeString("en-US", options)); 
// output: Wednesday, October 8, 2014 12:00:00 AM 
// what I'm looking for: Wednesday, October 8, 2014

How can I modify the options to not display time?

like image 345
SoluableNonagon Avatar asked Sep 03 '25 01:09

SoluableNonagon


1 Answers

Juste use toLocaleDateString instead of toLocaleTimeString and you should get the result you are expecting :

var date = new Date('2014', '9', '08'); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleDateString("en-US", options)); 

returns : "Wednesday, October 8, 2014"

I will also second the above poster and recommend using moment.js; it is lightweight and very flexible.

like image 88
Sycomor Avatar answered Sep 05 '25 15:09

Sycomor