CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #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.

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