Bootstrap 3 Datetimepicker 3.0.0 - 如何将一周的起始日设置为星期一

16

我在我的MVC 5项目中使用Bootstrap 3 Datetimepicker 3.0.0 的原因不多。

有没有办法调整一周的开始时间,使其从周一开始?语言标记也无效。

 $(function () {
    $('#PickupTime').datetimepicker({
     weekStart: 1
    });
 });

这不起作用是因为它不是相同的bootstrap-datapicker.js

9个回答

34
如果您使用的是从v2.8.1开始的moment.js版本,请在调用datetimepicker()之前添加以下代码。
moment.updateLocale('en', {
  week: { dow: 1 } // Monday is the first day of the week
});

$('#DateTime').datetimepicker();
如果您正在使用旧版本的moment.js,请执行以下操作。
moment.lang('en', {
  week: { dow: 1 }
});

$('#DateTime').datetimepicker();

这个按预期工作。在日历上显示星期一为第一天。除此之外,我需要从他们选择的任何日期中得到星期一和星期日。例如,如果他们选择了08/09/2018(MM / DD / YYYY),那么我需要08/06/2018和08/12/2018。为了获得这些日期,我使用了Weekday()函数。即moment(new Date()).weekday(6).format("MM/DD/YYYY"),它给了我本周的星期日。 https://momentjs.com/docs/#/get-set/weekday/ - Digvijayad

7
你也可以在调用datetimepicker()时通过locale覆盖dow值。
$('#myid').datetimepicker({
    format:'YYYY-MM-DD',
    locale:  moment.locale('en', {
        week: { dow: 1 }
    }),
});

1
工作得非常好!谢谢。 - JDSolutions

4
我刚刚完成了!在moment.js中更改这一行:
_week : {
    dow : 1, // Sunday is the first day of the week.
    doy : 6  // The week that contains Jan 1st is the first week of the year. 
},

"'dow'必须为1,而且一周从星期一开始。"

4

根据Datetimepicker的选项,这是不可能的。它只支持以下属性:

$.fn.datetimepicker.defaults = {
    pickDate: true,                 //en/disables the date picker
    pickTime: true,                 //en/disables the time picker
    useMinutes: true,               //en/disables the minutes picker
    useSeconds: true,               //en/disables the seconds picker
    useCurrent: true,               //when true, picker will set the value to the current date/time     
    minuteStepping:1,               //set the minute stepping
    minDate:`1/1/1900`,               //set a minimum date
    maxDate: ,     //set a maximum date (defaults to today +100 years)
    showToday: true,                 //shows the today indicator
    language:'en',                  //sets language locale
    defaultDate:"",                 //sets a default date, accepts js dates, strings and moment objects
    disabledDates:[],               //an array of dates that cannot be selected
    enabledDates:[],                //an array of dates that can be selected
    icons = {
        time: 'glyphicon glyphicon-time',
        date: 'glyphicon glyphicon-calendar',
        up:   'glyphicon glyphicon-chevron-up',
        down: 'glyphicon glyphicon-chevron-down'
    }
    useStrict: false,               //use "strict" when validating dates  
    sideBySide: false,              //show the date and time picker side by side
    daysOfWeekDisabled:[]          //for example use daysOfWeekDisabled: [0,6] to disable weekends 
};

您可以禁用周末,如果不想看到星期天的话。

源链接:http://eonasdan.github.io/bootstrap-datetimepicker/#options


但是左侧列仍然是星期日,人们期望星期一。 - HerGiz
在bootstrap-datapicker.js中,我已经偏移了名称,但只有名称被移动了,日期本身没有移动,所以整个日历不正确。甚至尝试在moment.js中更改,但是结果相同。 - HerGiz
有任何替代建议吗? - HerGiz
1
最终我转向了https://github.com/smalot/bootstrap-datetimepicker,但仍会关注第一个的开发。 - HerGiz
没有,它有漏洞,根本不起作用。回到原始状态。 - HerGiz

4

我使用了moment-with-langs.js替代了moment.js(我想它是ASP.NET MVC 5默认包的一部分)。

通过调用:

<script type="text/javascript">
    $('#DateTime').datetimepicker({
        language: "hr"
    });
</script>

事情终于有所进展,日历最终从星期一开始了。

更新: 更好的方法是在web.config中添加键。

<appSettings>    
    <add key="Culture" value="hr" />
</appSettings>

然后

$(document).ready(function () {
    $(document).on('focus', '#Date', function () {
        $(this).datetimepicker({
            locale: '@System.Configuration.ConfigurationManager.AppSettings["Culture"]',
            format: 'DD:MM:YYYY',
        });
    });
});

0
'locale' => [
    'firstDay' => 1
]

0

0
打开 jquery.datetimepicker.js 文件,找到变量 "dayOfWeekStart" 并将其设置为 1

2
OP指的是Eonasdan Bootstrap-Datepicker。这与jQuery的datepicker无关。 - Luís Cruz
与OP的问题无关。 - FrenkyB
$(".dataPicker").datetimepicker({ dayOfWeekStart: 1 }); $(".dataPicker").datetimepicker({ dayOfWeekStart: 1 }); - Grzegorz Miśkiewicz

0

我遇到了同样的问题,我正在使用:

并且,根据 user3928861 的答案,我在 moment.js 的第961行找到了答案,如下所示:

var defaultLocaleWeek = {
    dow : 1, // Sunday is the first day of the week.
    doy : 6  // The week that contains Jan 1st is the first week of the year.
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接