Work Week

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CKKwan

    #1

    Work Week

    Dear All,

    Anybody know how can I get the Work Week of a given date using CSharp?

    Example, in SQL Server we can get using DATEPART(ww, date); I need
    something equavalent.

    Thanks In Advance
  • =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?=

    #2
    Re: Work Week

    Hi CKKWan,

    check this out,...

    System.Globaliz ation.Gregorian Calendar gc = new
    System.Globaliz ation.Gregorian Calendar();
    int nWeek = gc.GetWeekOfYea r(DateTime.Now,
    System.Globaliz ation.CalendarW eekRule.FirstDa y, DayOfWeek.Monda y);
    MessageBox.Show (nWeek.ToString ());

    Regards

    Kerem


    --
    -----------------------
    Beste Grüsse / Best regards / Votre bien devoue
    Kerem Gümrükcü
    Latest Project: http://www.codeplex.com/restarts
    Latest Open-Source Projects: http://entwicklung.junetz.de
    -----------------------
    "This reply is provided as is, without warranty express or implied."
    "CKKwan" <ckkwan@my-deja.comschrieb im Newsbeitrag
    news:7af5b8bf-0733-4ab9-9604-ebdaf3bf00c5@u1 2g2000prd.googl egroups.com...
    Dear All,
    >
    Anybody know how can I get the Work Week of a given date using CSharp?
    >
    Example, in SQL Server we can get using DATEPART(ww, date); I need
    something equavalent.
    >
    Thanks In Advance

    Comment

    • Hans Kesting

      #3
      Re: Work Week

      CKKwan laid this down on his screen :
      Dear All,
      >
      Anybody know how can I get the Work Week of a given date using CSharp?
      >
      Example, in SQL Server we can get using DATEPART(ww, date); I need
      something equavalent.
      >
      Thanks In Advance
      //using System.Globaliz ation;

      CultureInfo local = new CultureInfo("nl-NL");

      Calendar cal = local.Calendar;
      int week = cal.GetWeekOfYe ar(DateTime.Now ,
      local.DateTimeF ormat.CalendarW eekRule,
      local.DateTimeF ormat.FirstDayO fWeek));

      Note: adjust the CultureInfo to your wishes.

      Hans Kesting


      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Work Week

        CKKwan wrote:
        Anybody know how can I get the Work Week of a given date using CSharp?
        >
        Example, in SQL Server we can get using DATEPART(ww, date); I need
        something equavalent.
        You already got a couple of replies pointing you to
        Calendar.GetWee kOfYear - but I will point out that
        if you are in Europe or other places that uses ISO
        weeks then that return wrong result in a few cases
        (at least at all Windows and .NET versions I have tried).
        It is easy to code a solution though.

        Arne

        Comment

        • Claes Bergefall

          #5
          Re: Work Week

          "Arne Vajhøj" <arne@vajhoej.d kwrote in message
          news:4862e51c$0 $90269$14726298 @news.sunsite.d k...
          CKKwan wrote:
          >Anybody know how can I get the Work Week of a given date using CSharp?
          >>
          >Example, in SQL Server we can get using DATEPART(ww, date); I need
          >something equavalent.
          >
          You already got a couple of replies pointing you to
          Calendar.GetWee kOfYear - but I will point out that
          if you are in Europe or other places that uses ISO
          weeks then that return wrong result in a few cases
          (at least at all Windows and .NET versions I have tried).
          It is easy to code a solution though.
          >
          Arne
          The bug (and workaround) that Arne is refering to is described in KB article
          Q200299 which you can find here:


          Here's the code for it:

          DateTimeFormatI nfo dateTimeFormat =
          CultureInfo.Cur rentCulture.Dat eTimeFormat;
          int week = dateTimeFormat. Calendar.GetWee kOfYear(yourDat e,
          dateTimeFormat. CalendarWeekRul e, dateTimeFormat. FirstDayOfWeek) ;
          int nextWeek = dateTimeFormat. Calendar.GetWee kOfYear(yourDat e.AddDays(7),
          dateTimeFormat. CalendarWeekRul e, dateTimeFormat. FirstDayOfWeek) ;
          if (week == 53 && nextWeek == 2)
          {
          week = 1;
          }


          /claes


          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: Work Week

            Claes Bergefall wrote:
            "Arne Vajhøj" <arne@vajhoej.d kwrote in message
            news:4862e51c$0 $90269$14726298 @news.sunsite.d k...
            >CKKwan wrote:
            >>Anybody know how can I get the Work Week of a given date using CSharp?
            >>>
            >>Example, in SQL Server we can get using DATEPART(ww, date); I need
            >>something equavalent.
            >You already got a couple of replies pointing you to
            >Calendar.GetWe ekOfYear - but I will point out that
            >if you are in Europe or other places that uses ISO
            >weeks then that return wrong result in a few cases
            >(at least at all Windows and .NET versions I have tried).
            >It is easy to code a solution though.
            >
            The bug (and workaround) that Arne is refering to is described in KB article
            Q200299 which you can find here:

            >
            Here's the code for it:
            >
            DateTimeFormatI nfo dateTimeFormat =
            CultureInfo.Cur rentCulture.Dat eTimeFormat;
            int week = dateTimeFormat. Calendar.GetWee kOfYear(yourDat e,
            dateTimeFormat. CalendarWeekRul e, dateTimeFormat. FirstDayOfWeek) ;
            int nextWeek = dateTimeFormat. Calendar.GetWee kOfYear(yourDat e.AddDays(7),
            dateTimeFormat. CalendarWeekRul e, dateTimeFormat. FirstDayOfWeek) ;
            if (week == 53 && nextWeek == 2)
            {
            week = 1;
            }
            It was the bug I was refeering to, but for workaround I tend
            to prefer to DIY.

            public static int WeekNumber(int year, int mon, int day)
            {
            int a = (14 - mon) / 12;
            int y = year + 4800 - a;
            int m = mon + 12*a - 3;
            int JD = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 +
            y/400 - 32045;
            int d4 = (((JD + 31741 - JD % 7) % 146097) % 36524) % 1461;
            int L = d4 / 1460;
            int d1 = ((d4 - L) % 365) + L;
            return d1 / 7 + 1;
            }

            Arne

            Comment

            Working...