Flex,如何获取约会的一年中的第几周?

发布于 2024-08-23 15:30:25 字数 115 浏览 6 评论 0原文

我无法在 Flex Date 对象中找到获取一年中的第几周(1 到 52)的方法,

找到这个的最佳方法是什么?对于像 Java 中的 JodaTime 这样的日期操作,是否有任何有用的 Flex 库?

I could not find a method in Flex Date object to get the week of year (1 to 52)

What is the best way to find this? Is there any useful library for flex for such date operations like JodaTime in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思慕 2024-08-30 15:30:25

我不知道有什么库,但这个函数将为您提供周索引(从零开始)。

function getWeek(date:Date):Number
{
  var days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
  var year:Number = date.fullYearUTC;
  var isLeap:Boolean = (year % 4 == 0) && (year % 100 != 0)
                       || (year % 100 == 0) && (year % 400 == 0); 
  if(isLeap)
    days[1]++;

  var d = 0;
  //month is conveniently 0 indexed.
  for(var i = 0; i < date.month; i++)
    d += days[i];
  d += date.dateUTC;

  var temp:Date = new Date(year, 0, 1);
  var jan1:Number = temp.dayUTC;
  /**
   * If Jan 1st is a Friday (as in 2010), does Mon, 4th Jan 
   * fall in the first week or the second one? 
   *
   * Comment the next line to make it in first week
   * This will effectively make the week start on Friday 
   * or whatever day Jan 1st of that year is.
   **/
  d += jan1;

  return int(d / 7);
}

I'm not aware of a library, but this function will get you the week index (zero based).

function getWeek(date:Date):Number
{
  var days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
  var year:Number = date.fullYearUTC;
  var isLeap:Boolean = (year % 4 == 0) && (year % 100 != 0)
                       || (year % 100 == 0) && (year % 400 == 0); 
  if(isLeap)
    days[1]++;

  var d = 0;
  //month is conveniently 0 indexed.
  for(var i = 0; i < date.month; i++)
    d += days[i];
  d += date.dateUTC;

  var temp:Date = new Date(year, 0, 1);
  var jan1:Number = temp.dayUTC;
  /**
   * If Jan 1st is a Friday (as in 2010), does Mon, 4th Jan 
   * fall in the first week or the second one? 
   *
   * Comment the next line to make it in first week
   * This will effectively make the week start on Friday 
   * or whatever day Jan 1st of that year is.
   **/
  d += jan1;

  return int(d / 7);
}
淡紫姑娘! 2024-08-30 15:30:25

我只是想指出上面的解决方案有一个错误。

for(var i = 0; i < date.month; i++)

应该是

for(var i = 0; i < date.monthUTC; i++)

为了正常工作。

尽管如此,还是感谢您的解决方案,它对我帮助很大:)

I just want to point out that there is an error in the above solution.

for(var i = 0; i < date.month; i++)

should be

for(var i = 0; i < date.monthUTC; i++)

in order to work properly.

Nevertheless thanks for the solution, it helped me a lot :)

梦幻的心爱 2024-08-30 15:30:25

d 除以 7 之前,应先减 1。否则,周六将转到下周。

以2011年为例,2011年1月1日是星期六。它应该是第 0 周,1/8/2011 应该是第 1 周。

如果 d 不递减,
那么1+6=7/7=1,8+6=14/7=2。所以这些都是不正确的。

Before d is divided by 7, it should be decremented by 1. Otherwise, Saturday would go to next week.

Take 2011 as example, 1/1/2011 is Saturday. It should be week in 0, and 1/8/2011 should be in week 1.

If d is not decremented,
then 1+6=7/7=1, and 8+6=14/7=2. So these are not correct.

梦开始←不甜 2024-08-30 15:30:25

我尝试使用 Amargosh 的函数,但我遇到了 UTC 值的问题。
以及一年的第一天。

因此我修改了jan1的设置(取决于周日)
最后一周的计算

这是我使用的函数,基于 Amargosh 的函数:

public static function getWeek(date:Date):String
{
    var days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var year:Number = date.fullYear;
    var isLeap:Boolean = (year % 4 == 0) && (year % 100 != 0)
        || (year % 100 == 0) && (year % 400 == 0); 
    if(isLeap)
        days[1]++;

    var d:Number = 0;

    for(var i:int = 0; i < (date.month); i++){
        d += days[i];
    }

    d += date.date;

    var temp:Date = new Date(year, 0, 1);
    var jan1:Number = temp.day;
    if(jan1 == 0) // sunday
        jan1 = 7;
    d += jan1 - 1;


    var week:int = int((d-1) / 7);

    if(week == 0) // les premiers jours de l'année
        week = 52;

    return (week < 10 ? "0" : "") + week;

}

I tried to use Amarghosh's function but I had issues with the UTC values.
And with the first days of a year as well.

Therefore I modified the setting of jan1 (depending on sundays)
and the final week calculation

Here is the function I use, based on Amarghosh's one :

public static function getWeek(date:Date):String
{
    var days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var year:Number = date.fullYear;
    var isLeap:Boolean = (year % 4 == 0) && (year % 100 != 0)
        || (year % 100 == 0) && (year % 400 == 0); 
    if(isLeap)
        days[1]++;

    var d:Number = 0;

    for(var i:int = 0; i < (date.month); i++){
        d += days[i];
    }

    d += date.date;

    var temp:Date = new Date(year, 0, 1);
    var jan1:Number = temp.day;
    if(jan1 == 0) // sunday
        jan1 = 7;
    d += jan1 - 1;


    var week:int = int((d-1) / 7);

    if(week == 0) // les premiers jours de l'année
        week = 52;

    return (week < 10 ? "0" : "") + week;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文