如何在AS3中的特定日期开始显示当前周数?

时间:2016-03-30 08:19:23

标签: actionscript-3 date

我想显示一周的数字,但是从星期三开始。你知道我怎么能这样做?

示例: this_week是13.

下周三,周数将是14(周二仍然是第13周)。

目前我的代码是:

function getWeekOfTheYear(d:Date):uint{
    var firstDay:Date = new Date(d.getFullYear(), 0, 1);
    var dayOffset:uint = 9 - firstDay.getDay();
    var firstMonday:Date = new Date(d.getFullYear(), 0, (dayOffset > 7) ? dayOffset - 7 : dayOffset);
    var currentDay:Date = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    var weekNumber:uint = (millisecondsToDays(currentDay.getTime() - firstMonday.getTime()) / 7) + 1;

    return (weekNumber == 0) ? getWeekOfTheYear(new Date(d.getFullYear() - 1, 11, 31)) : weekNumber;
    trace("week number is"+weekNumber);
}

但它是我在github上找到的功能并计算每个星期一的星期数...

Thx求助,

1 个答案:

答案 0 :(得分:0)

如果我理解正确,此代码将从星期三开始返回周数。

function getWeekOfTheYear(d:Date):uint{
    var firstDay:Date = new Date(d.getFullYear(), 0, 1);
    //var dayOffset:uint = 9 - firstDay.getDay();
    //var firstMonday:Date = new Date(d.getFullYear(), 0, (dayOffset > 7) ? dayOffset - 7 : dayOffset);

    var dayOffset2:uint = 11 - firstDay.getDay();
    var firstWednesday:Date = new Date(d.getFullYear(), 0, (dayOffset2 > 7) ? dayOffset2 - 7 : dayOffset2);


    var currentDay:Date = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    var weekNumber:uint = (millisecondsToDays(currentDay.getTime() - firstWednesday.getTime()) / 7) + 1;

    return (weekNumber == 0) ? getWeekOfTheYear(new Date(d.getFullYear() - 1, 11, 31)) : weekNumber;
    trace("week number is"+weekNumber);
}

以下代码返回特定日期开始的周数。

static var Sun:int = 0;
static var Mon:int = 1;
static var Tue:int = 2;
static var Wed:int = 3;
static var Thu:int = 4;
static var Fri:int = 5;
static var Sat:int = 6;
function getWeekOfTheYear(d:Date, startday:int):uint{
    var firstDay:Date = new Date(d.getFullYear(), 0, 1);
    var dayOffset:uint = (7 + startday+ 1) - firstDay.getDay();
    var firstSpecificDay:Date = new Date(d.getFullYear(), 0, (dayOffset > 7) ? dayOffset - 7 : dayOffset);

    var currentDay:Date = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    var weekNumber:uint = (millisecondsToDays(currentDay.getTime() - firstSpecificDay.getTime()) / 7) + 1;

    return (weekNumber == 0) ? getWeekOfTheYear(new Date(d.getFullYear() - 1, 11, 31), startday) : weekNumber;
}

如何使用

trace(getWeekOfTheYear(new Date("2016/04/05"), Wed));   // Weeknumber starting on Wednesday
trace(getWeekOfTheYear(new Date("2016/04/06"), Wed));