Moment.js一周中的第一天不正确 [英] Moment.js first day of the week incorrect
问题描述
我第一次使用Moment.js.我尝试检索一周中第一天的日期,而在欧洲,通常是星期一.无论我做什么,我都会在一周的第一天得到星期日.
I'm working with Moment.js for my first time. I tried to retrieve the date of the first day of the week, and in Europe this is normally Monday. Whatever I do, I get Sunday as result of the first day of the week.
我尝试设置不同的语言(本地或全局),但无济于事.我使用了 moment.js github页面中的langs.min.js文件. "en-gb"和"fr" 的语言文件包含以下代码行:
I tried to set-up different languages (local or globally), but to no avail. I use the langs.min.js file from the moment.js github page. The language file of "en-gb" and "fr" has the line of code:
dow : 1, // Monday is the first day of the week.
因此,当我要求每周的第一天时,我会得到星期一的日期吗?我一直以星期日为输出.
So I would get the date of monday when I ask for the first day of the week right? I keep getting Sunday as output.
// Create moment object
var localLang = moment();
// Set language to french
localLang.lang('fr');
// Test language
localLang.lang(); // Output: fr
// Retrieve first day of the week and format it
var dow = localLang.startOf('week').format('dddd DD-MM-YYYY'); // Output: dimanche 14-04-2013
Dimanche是星期天的法语.如您所见,moment.js可以成功使用语言文件,但不使用星期几配置
Dimanche is french for Sunday.. As you see, moment.js can use the language file succesfully but doesn't use the day of the week configuration
JSfiddle与moment.js和langs.js进行测试: JSFiddle
JSfiddle with moment.js and langs.js to test: JSFiddle
我可以用day(1)
而不是startOf('week')
来获取星期一而不是星期日的日期.但是使用day(0)
我仍然可以得到星期日.为什么不按语言文件中的配置在一周的第一天星期一.
edit:
I can get the date of Monday instead of Sunday with day(1)
instead of startOf('week')
. But using day(0)
I still get Sunday as a result. Why isn't monday the first day of the week, as configured in the language files.
推荐答案
对于最近遇到此问题的任何人,现在都支持lang
方法来设置特定于语言环境的设置.
For anyone who come across this question lately, moment now support lang
method to set locale specific setting.
将第一周设置为星期一:
Setting first week to be Monday:
moment.lang('zh-cn', {
week : {
dow : 1 // Monday is the first day of the week
}
});
var date = moment().weekday(0); // date now is the first day of the week, (i.e., Monday)
这篇关于Moment.js一周中的第一天不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!