How can I get a specific date format using momentjs?
I want to display the date pressed in a specific format with localized strings, but even though it shows the locale set, the string I get is this:
Fri Jun 05 2015 00:00:00 GMT+0300 (EEST)
I would like to turn this into Friday, 5. June 2015
with localized words. On clickEvents when I am console.log
target
, I get the wrong string under date._d
, although in date._locale
the date I also see localized stuff. And I really don't understand how to change the format when I use target.date.toDate()
. My click Events in CLNDR initiative:
clickEvents: {
click: function (target) {
$('.selected-date').text(target.date.toDate());
console.log(target);
}
},
Not sure if this is a moment or CLNDR issue, but I'm leaning towards a moment as CLNDR uses it.
And here's what comes back console.log
:
{
element: div.day.today.calendar-day-2015-06-05.calendar-dow-4,
events: Array[0], date: Moment,
date: Moment {
_d: Fri Jun 05 2015 00:00:00 GMT+0300 (EEST)
_f: "YYYY-MM-DD "
_i: "2015-06-05"
_isAMomentObject: true
_isUTC: false
_locale: Locale
_pf: Object
__proto__: Moment
element: div.day.today.calendar-day-2015-06-05.calendar-dow-4
events: Array[0]
__proto__: Object
}
}
Something like that. Any help was appreciated.
You cannot "flip your own" when it comes to localized dates, because each locale has its own set of localized date strings. This is due to the fact that different countries show their dates differently.
Why don't you want to use the country format? If someone from another country uses your site, they will find the other date format odd.
You can get what you are looking for:
moment(date).format('dddd, LL');
For the US, this will print
Thursday, September 4 1986
source to share