CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    63

    Talking Get First Day of week

    Hi all,
    i want get the first day of the week from today date. How can i do this?
    Thank's

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Get First Day of week

    Quote Originally Posted by dewoul
    Hi all,
    i want get the first day of the week from today date. How can i do this?
    Thank's
    hi,

    Do you mean how to get the first date of the current week?

    Code:
    System.DateTime today = System.DateTime.Now;
               
    int dayOfWeek = (int)today.DayOfWeek;
    
    System.DateTime firstDayOfWeek = today.Subtract(new TimeSpan(dayOfWeek, 0, 0, 0));
    this assumes that sunday is first day of week. If you want to have monday as first day:

    Code:
    System.DateTime today = System.DateTime.Now;
               
    int dayOfWeek = (int)today.DayOfWeek-1;
    
    if (dayOfWeek ==-1)
         dayOfWeek = 7;
    
    
    System.DateTime firstDayOfWeek = today.Subtract(new TimeSpan(dayOfWeek, 0, 0, 0));
    Regards,

    Laitinen
    Last edited by laitinen; February 26th, 2007 at 12:14 PM.

  3. #3
    Join Date
    Jan 2007
    Posts
    491

    Re: Get First Day of week

    The method DayOfWeek of DateTime object
    Code:
                DateTime current = DateTime.Now;
                MessageBox.Show(current.DayOfWeek.ToString());

  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Get First Day of week

    Quote Originally Posted by Talikag
    The method DayOfWeek of DateTime object
    Code:
                DateTime current = DateTime.Now;
                MessageBox.Show(current.DayOfWeek.ToString());
    This does not get the first day of week. See post #2.

    Regards,

    Laitinen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured