开发者

Get current calendar week

What is the common method, to get the current开发者_运维技巧 calendar week in actionscript-3?


It might be a good idea to re-cast the problem so you can get the day number in the current year, you'll be able to use it for more date based calculations if you have to. That's most easily done with a static array of ints and a bit of arithmetic...

public static var month_days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

public static day_num(Date d):int
{
  var cumulative_days:int = 0;
  for (int i = 0; i < d.month; i++)
  {
    cumulative_days += month_days[i];
  }
  cumulative_days += d.date;

  if (add_leap_day(d)) cumulative_days++;

  return cumulative_days;
}

public static week_num(Date d):int
{
  var int day_num = day_num(d);
  return Math.floor(day_num/7);
}

public static function add_leap_day(Date d):boolean
{
  // I'll let you work this out for yourself
  // bear in mind you don't just need to know whether it's a leap year...
}

Beware of a few things:

  • Does your definition of a calendar week start on 1st Jan or the first Sunday of the year?
  • What do you do about the spare days in the year, especially leap years? (52x7 != 365)
  • Are you interested in the calendar or fiscal year?
  • Is the definition of a week the same for every week of your year? Sometimes Christmas has two weeks or an extra month!

I wound up writing a whole library of date based arithmetic functions for fiscal and regular calendars. It is a tricky problem if you are operating in any environment where your user has their own version of the calendar - i.e. any financial application at all.


Or get your hands on org.casalib.util.DateUtil and use method getWeekOfTheYear(d:Date):uint.


Here is one another function that returns the starting date of the current week:

public static function getDayCount(year:int, month:int):int
{
    var d:Date = new Date(year, month + 1, 0);
    return d.getDate();
}

public static function getThisWeekStartDate(date:Date):Date
{
    var weekStartDate:Date;

    var currentDateDay:Number = date.day;
    if(currentDateDay == 0)
    {
        weekStartDate = new Date(date.fullYear, date.month, date.date);
    }
    else
    {
        var sDate:Number = date.date - currentDateDay;
        if(sDate < 0)
        {
            var newWeekStartDate:Number = sDate + getDayCount(date.fullYear, date.month);
            weekStartDate = new Date(date.fullYear, date.month-1, newWeekStartDate);
        }
        else
        {
            weekStartDate = new Date(date.fullYear, date.month, sDate);
        }
    }

    return weekStartDate;
}

And you can get End Date of week by :

var endDate:Date = new Date(startDate.fullYear, startDate.month, startDate.date);
endDate.date += 6;


var today:Date = new Date();
var sunday:Date = new Date(today.fullYear, today.month, today.date - today.day);
var saturday:Date = new Date(sunday.fullYear, sunday.month, sunday.date + 6);
trace(sunday);
trace(saturday);

//Sun Oct 26 00:00:00 GMT+0800 2014
//Sat Nov 1 00:00:00 GMT+0800 2014
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜