CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2017
    Posts
    3

    Question Assign DayOfWeek as variable

    Here is my code which calculate number of days between two dates except weekends. which worked fine but need help to assign DayOfWeek as variable like:
    Code:
    daya = "Friday" 
    dayb = "Saturday"
    
    DayOfWeek of the week change some time Sunday, etc..
    
    Dim sdate As Date = DateTime.Parse(txtfdate.Text).ToString("MM/dd/yyyy")
    Dim edate As Date = DateTime.Parse(txttdate.Text).ToString("MM/dd/yyyy")
    Dim RqHours = 0
    Dim totalDays = (edate - sdate).Days
    
    Dim daya As string = "Friday" 
    Dim dayb As string= "Saturday"
    
    For i = 0 To totalDays
    Dim weekday As DayOfWeek = sdate.AddDays(i).DayOfWeek
    If weekday <> DayOfWeek.Friday AndAlso weekday <> DayOfWeek.Saturday Then
    'If weekday <> daya AndAlso weekday <> dayb Then
    RqHours += 1
    End If
    Next
    Last edited by 2kaud; July 13th, 2017 at 03:47 PM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Assign DayOfWeek as variable

    What is the problem?

  3. #3
    Join Date
    Jul 2017
    Posts
    3

    Re: Assign DayOfWeek as variable

    Quote Originally Posted by Arjay View Post
    What is the problem?
    The following code working well:
    Code:
    For i = 0 To totalDays
    
    Dim weekday As DayOfWeek = sdate.AddDays(i).DayOfWeek
    If weekday <> DayOfWeek.Friday AndAlso weekday <> DayOfWeek.Saturday Then
    RqHours += 1
    
    End If
    Next
    here is got error:
    Code:
    Dim daya As string = "Friday" 
    Dim dayb As string= "Saturday"
    
    For i = 0 To totalDays
    
    Dim weekday As DayOfWeek = sdate.AddDays(i).DayOfWeek
    If weekday <> daya AndAlso weekday <> dayb  Then
    RqHours += 1
    
    End If
    Next
    Last edited by 2kaud; July 14th, 2017 at 09:41 AM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Assign DayOfWeek as variable

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#'].

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Assign DayOfWeek as variable

    Quote Originally Posted by kmafsar View Post
    The following code working well:
    Code:
    For i = 0 To totalDays
    
    Dim weekday As DayOfWeek = sdate.AddDays(i).DayOfWeek
    If weekday <> DayOfWeek.Friday AndAlso weekday <> DayOfWeek.Saturday Then
    RqHours += 1
    
    End If
    Next
    here is got error:
    Code:
    Dim daya As string = "Friday" 
    Dim dayb As string= "Saturday"
    
    For i = 0 To totalDays
    
    Dim weekday As DayOfWeek = sdate.AddDays(i).DayOfWeek
    If weekday <> daya AndAlso weekday <> dayb  Then
    RqHours += 1
    
    End If
    Next
    What is the error? Does the code not compile or is it a runtime error? If the code doesn't compile, then you have a mismatched type between the strings defined as "Friday" and "Saturday" and the DayOfWeek enum values (enums are defined as integers).

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