moment.locale 增加额外的一天
moment.locale adding extra day
我尝试将 moment.locale
添加到我的代码中:
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";
}
}
一切正常,文本月份以法语显示,但问题是moment.locale
增加了一天
示例:在 moment.locale
之前,我的日期显示为 9 月 6 日(英文),但在添加 moment.locale
之后显示为 7 Septembre(法文)
为什么要增加一天?
自己解决的,去掉.add(1,'day')即可
据我了解,那么在星期天开始的一周之前,需要添加 1 天才能得到星期一,但是更改语言环境周从星期一开始,所以不需要添加额外的一天
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";
}
}
时间过时了,它只会给你的包增加很多额外的重量。
JavaScript自己有能力做更多更好的事情
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)
我尝试将 moment.locale
添加到我的代码中:
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";
}
}
一切正常,文本月份以法语显示,但问题是moment.locale
增加了一天
示例:在 moment.locale
之前,我的日期显示为 9 月 6 日(英文),但在添加 moment.locale
之后显示为 7 Septembre(法文)
为什么要增加一天?
自己解决的,去掉.add(1,'day')即可 据我了解,那么在星期天开始的一周之前,需要添加 1 天才能得到星期一,但是更改语言环境周从星期一开始,所以不需要添加额外的一天
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";
}
}
时间过时了,它只会给你的包增加很多额外的重量。 JavaScript自己有能力做更多更好的事情
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)