Reputation: 11755
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?
Upvotes: 4
Views: 4469
Reputation: 1272
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.
Upvotes: 10
Reputation: 241525
If you are satisfied with allowing the browser to determine the display format, you can use the toDateString
or toLocaleDateString
functions of the Date
object. You will get different results on different browsers.
Otherwise, consider using a library such as moment.js, so you can control the format to your liking. For example:
moment([2014,9,8]).format('dddd, MMMM Do, YYYY') // "Wednesday, October 8, 2014"
Upvotes: 0