2010-06-12 146 views
0

什麼是常見方法,以獲得當前日曆周獲取當前日曆周

+0

您是否試圖獲得本週的日期範圍(週日@12:00-週六@ 11:59 PM)或週數? – 2010-06-12 14:49:48

+0

這已經在這裏回答http://stackoverflow.com/questions/2362956/flex-how-to-get-week-of-year-for-a-date/2363000#2363000 – Amarghosh 2010-06-14 10:50:57

回答

1

重新設置問題以便您可以獲得當年的日期編號可能是個好主意,如果必須的話,您可以將其用於更多基於日期的計算。這是最容易與INTS的靜態數組和位運算的做...

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... 
} 

當心的幾件事情:

  • 貴上1月1日或日曆星期開始的定義今年的第一個星期天?
  • 你對一年中的閒暇時間,特別是閏年做什麼? (52x7!= 365)
  • 您對日曆或財政年度感興趣嗎?
  • 一年的每週定義是否相同?有時聖誕節有兩個星期或一個多月!

我結束了爲財務日曆和常規日曆編寫基於日期的算術函數庫。如果您在用戶擁有自己版本的日曆的任何環境中工作,即任何財務應用程序,這都是一個棘手的問題。

0

這裏是一個返回當前周的開始日期的另一個功能:

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; 
} 

,您可以通過獲得一週的結束日期:

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

我想你錯了,currentDateDay == 0實際上是星期天 – 2013-10-07 10:08:49

0
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 
1

或者讓你的手在org.casalib.util.DateUtil和使用方法getWeekOfTheYear(d:Date):uint

相關問題