Day Of Week

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

    #1

    Day Of Week

    ..NET enumerates the day of the week as 0-6 ( Sunday to Saturday ). This
    seems to remain the same regardless of the culture settings.

    Just wondering if this is alterable is some way, and if not how you guys
    work out for example, next Sunday. I mean sure, I can create a function
    which will allways return the number of days between today and sunday using
    for example the day name and a select statement, but it seems too complex.

    Any ideas on the simple approach ?


  • Just Me

    #2
    Re: Day Of Week

    Dont know if its the best way, but I solved it like this,

    public static DateTime thisSunday(Date Time targetDate)

    {

    int realDay = 1;

    if (targetDate.Day OfWeek == 0) realDay = 7; else realDay =
    (int)targetDate .DayOfWeek;

    return targetDate.AddD ays(7 - realDay);

    }



    "Just Me" <news.microsoft .comwrote in message
    news:%230cvqb3h IHA.1204@TK2MSF TNGP03.phx.gbl. ..
    .NET enumerates the day of the week as 0-6 ( Sunday to Saturday ). This
    seems to remain the same regardless of the culture settings.
    >
    Just wondering if this is alterable is some way, and if not how you guys
    work out for example, next Sunday. I mean sure, I can create a function
    which will allways return the number of days between today and sunday
    using for example the day name and a select statement, but it seems too
    complex.
    >
    Any ideas on the simple approach ?
    >

    Comment

    • Stan

      #3
      Re: Day Of Week

      Hi

      The problem is not that the enumeration is 0 - 6. The complication
      arises because you apparently require a logically different result for
      Sunday than for other days of the week.

      According to your solution if targetDate is Monday to Saturday then
      you want to return the date for the *next* Sunday whereas if
      targetDate is Sunday then you want to return the date unaltered i.e.
      *this* Sunday.

      For example if the code were written thus:

      DateTime NextSunday(Date Time targetDate)
      {
      int dow = (int)targetDate .DayOfWeek;
      return targetDate.AddD ays(7 - dow);
      }

      the result would be the same as your solution except when targetDate
      is Sunday, in which case you get next Sunday i.e. a week later.

      Comment

      • Just Me

        #4
        Re: Day Of Week

        Correct !

        Thanks for your input.


        "Stan" <google@philpha ll.me.ukwrote in message
        news:6f31b7c6-894f-415c-9f0c-41adbb65ee60@e1 0g2000prf.googl egroups.com...
        Hi
        >
        The problem is not that the enumeration is 0 - 6. The complication
        arises because you apparently require a logically different result for
        Sunday than for other days of the week.
        >
        According to your solution if targetDate is Monday to Saturday then
        you want to return the date for the *next* Sunday whereas if
        targetDate is Sunday then you want to return the date unaltered i.e.
        *this* Sunday.
        >
        For example if the code were written thus:
        >
        DateTime NextSunday(Date Time targetDate)
        {
        int dow = (int)targetDate .DayOfWeek;
        return targetDate.AddD ays(7 - dow);
        }
        >
        the result would be the same as your solution except when targetDate
        is Sunday, in which case you get next Sunday i.e. a week later.

        Comment

        Working...