PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 [RESOLVED] DateTime-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] DateTime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    102

    Resolved [RESOLVED] DateTime

    hello , am using ASP.NET c#

    am trying calc 2 datetime from textbox , and I want add weekend days only Frieday (off )

    so if I calc from Sat 1/10 to Sat 7/10
    result well be 6 days
    Not 7 days

    Code:
    DateTime dt1 = Convert.ToDateTime(TextBox12.Text);
                DateTime dt2 = Convert.ToDateTime(TextBox13.Text);
                TimeSpan ts = dt1.Subtract(dt1);
                int NoOfDays = dt2.Day;
                TextBox29.Text = NoOfDays.ToString();

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,112

    Re: DateTime

    Use a loop to increment the date by one day at a time and then add 1 for each day you're interested in. For instance, here's a method that counts the number of weekdays from one date to another:
    vb.net Code:
    1. Public Function CountWeekDays(startDate As Date, endDate As Date) As Integer
    2.     Dim currentDate = startDate
    3.     Dim dayCount = 0
    4.  
    5.     Do
    6.         Select Case currentDate.DayOfWeek
    7.             Case DayOfWeek.Saturday, DayOfWeek.Sunday
    8.                 'Ignore weekends
    9.             Case Else
    10.                 dayCount += 1
    11.         End Select
    12.  
    13.         currentDate = currentDate.AddDays(1)
    14.     Loop Until currentDate > endDate
    15.  
    16.     Return dayCount
    17. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    102

    Re: DateTime

    Quote Originally Posted by jmcilhinney View Post
    Use a loop to increment the date by one day at a time and then add 1 for each day you're interested in. For instance, here's a method that counts the number of weekdays from one date to another:
    vb.net Code:
    1. Public Function CountWeekDays(startDate As Date, endDate As Date) As Integer
    2.     Dim currentDate = startDate
    3.     Dim dayCount = 0
    4.  
    5.     Do
    6.         Select Case currentDate.DayOfWeek
    7.             Case DayOfWeek.Saturday, DayOfWeek.Sunday
    8.                 'Ignore weekends
    9.             Case Else
    10.                 dayCount += 1
    11.         End Select
    12.  
    13.         currentDate = currentDate.AddDays(1)
    14.     Loop Until currentDate > endDate
    15.  
    16.     Return dayCount
    17. End Function
    thank jmc, can I get the code by C# asp.net Please.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,112

    Re: DateTime

    My apologies. I'm so used to writing Vb code most of the time here that I missed the fact that your code was C#.
    csharp Code:
    1. public int CountWeekDays(DateTime startDate, DateTime endDate)
    2. {
    3.     var currentDate = startDate;
    4.     var dayCount = 0;
    5.  
    6.     do
    7.     {
    8.         switch (currentDate.DayOfWeek)
    9.         {
    10.             case DayOfWeek.Saturday:
    11.             case DayOfWeek.Sunday:
    12.                 // Ignore weekends
    13.                 break;
    14.             default:
    15.                 dayCount++;
    16.                 break;
    17.         }
    18.  
    19.         currentDate = currentDate.AddDays(1);
    20.     } while (currentDate <= endDate);
    21.  
    22.     return dayCount;
    23. }

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,112

    Re: DateTime

    By the way, it would have been more appropriate to post this question in the C# forum as it has nothing specifically to do with ASP.NET. Not a big deal but the code would be the same whether for ASP.NET, WinForms or in a Console app, which is where I wrote that code.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    102

    Re: DateTime

    Quote Originally Posted by jmcilhinney View Post
    By the way, it would have been more appropriate to post this question in the C# forum as it has nothing specifically to do with ASP.NET. Not a big deal but the code would be the same whether for ASP.NET, WinForms or in a Console app, which is where I wrote that code.
    that's help me

    thank you very much I did make it finally


    Code:
     protected void Button29_Click(object sender, EventArgs e)
            {
                
                    DateTime d1 = Convert.ToDateTime(TextBox12.Text);
                    DateTime d2 = Convert.ToDateTime(TextBox13.Text);
                    var totalDays = (d2 - d1).Days;
                    int wd = 0;
    
                    for (int x = 0; x <= totalDays; x++)
                    {
                        DayOfWeek weekday = d1.AddDays(x).DayOfWeek;
                        if (weekday != DayOfWeek.Friday)
                            wd += 1;
                    }
    
    
                    Label1.Text = wd.ToString();
                }
            }
        }
    Name:  2019-10-31_21-50-13.png
Views: 70
Size:  11.9 KB
    Last edited by kankon; Oct 31st, 2019 at 01:51 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
  •  



Featured


Click Here to Expand Forum to Full Width