Reputation: 21
I try to add moment.locale
to my code:
if(time_to_exe == 6)
{
moment.locale('fr', {
months : 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_')});
var orderDateTime = moment();
// Get Sunday (first day) of this week and add 3 days (to get to Wednesday) and set the time to 11:59am
var cutOffDate = moment().startOf('week').add(3,'days').set({'hour': 11, 'minute': 59, 'second': 59});
// Initialize delivery date from order date
var deliveryDate = orderDateTime.clone();
if (orderDateTime.isSameOrBefore(cutOffDate)) {
deliveryDate = deliveryDate.add(1,'week').startOf('week').add(1,'day'); // Monday next week
} else {
deliveryDate = deliveryDate.add(2,'week').startOf('week').add(1,'day'); // Monday the week after next
}
if (deliveryDate) {
time_to_exe = deliveryDate.format("D MMMM");
} else {
time_to_exe = time_to_exe + "d";
}
}
Everything working fine, textual month showing in French, but the problem is that moment.locale
add an extra day
Example: before moment.locale
my date show 6 September (in English), but after an added moment.locale
it's showing 7 Septembre (in French)
Why it's adding an extra day?
Upvotes: 0
Views: 83
Reputation: 4252
Moment is old and it only adds a lot extra of weight to yow bundle. JavaScript is capable to do more and better things by its own
const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('de-DE', options));
// expected output: Donnerstag, 20. Dezember 2012
console.log(event.toLocaleDateString('ar-EG', options));
// expected output: الخميس، ٢٠ ديسمبر، ٢٠١٢
console.log(event.toLocaleDateString(undefined, options));
// expected output: Thursday, December 20, 2012 (varies according to default locale)
Learn more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
Upvotes: 1
Reputation: 21
Resolved by myself, just remove .add(1,'day') As I understand, then before a week started with the Sunday, and the needed to add 1 day to get Monday, but change locale week start with the Monday, so there is no need add an extra day
if(time_to_exe == 6)
{
var orderDateTime = moment();
// Get Sunday (first day) of this week and add 3 days (to get to Wednesday) and set the time to 11:59am
var cutOffDate = moment().startOf('week').add(2,'days').set({'hour': 11, 'minute': 59, 'second': 59});
// Initialize delivery date from order date
var deliveryDate = orderDateTime.clone();
if (orderDateTime.isSameOrBefore(cutOffDate)) {
deliveryDate = deliveryDate.add(1,'week').startOf('week'); // Monday next week
} else {
deliveryDate = deliveryDate.add(2,'week').startOf('week'); // Monday the week after next
}
if (deliveryDate) {
time_to_exe = deliveryDate.format("D. MMMM");
} else {
time_to_exe = time_to_exe + "d";
}
}
Upvotes: 0