在zebra datepicker中如何从所选日期获取或提取周数?

下面是我的尝试,但它不起作用。 第一行是初始化datepicker,然后有一些选项可以设置,然后在onSelect部分是我如何尝试获取所选并转换为周数,然后进行测试我只是试图提醒周数。 一旦我开始工作,我会将周数放在某处使用。

$("input#someid").Zebra_DatePicker({
format: 'm-d-Y',
direction: 1,
show_clear_date: 'FALSE',
onSelect: function(view, elements) { //when a date is selected
    Date.prototype.getWeek = function() {
        var onejan = new Date(this.getFullYear(),0,1);
        return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
    }
    var myvariable = $( "#someid" ).val();
    var weekno = myvariable.getWeek();
    alert(weekno);
}
});

日期的格式很重要。 它需要能够从mdY(01-13-2014)格式转换为周数。 例如,今天的日期将有3的结果,因为它是一年中的第3周。

采纳答案:

我不确定Zebra_DatePicker,它是否给出了周数,但即使它没有直接给你一周的数字,如果你有日期和月份,你可以自己解决...例如这种方式

week = math.ceil((month * day)/7);

这将为您提供一年中的一周。 另一方面,如果你甚至无法得到日期和月份,那么通过分割日期来获得它。

var now = new Date(document.getElementById('element_id').value);//replace the element_id with actual input id of the zebra datepicker

//var now = new Date("02-01-2014");//hard coded date
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var week = Math.ceil(day/7);
alert(week);

干杯

PS假设第三周从第15周开始吧?

author: omicans

参考更多解答: in zebra datepicker how can i get or extract week number from selected date? ,转载请保留出处在zebra datepicker中如何从所选日期获取或提取周数?及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:datepicker