Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Counting the Number of Working Days

Counting the Number of Working Days

Scheduled Pinned Locked Moved C#
csharptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zoroyster
    wrote on last edited by
    #1

    Hi I'm creating a C# program to measure differences between dates. Ive figured out how to measure the difference between two dates using DateTime and TimeSpan, but is there any short cut way on how I will be able to measure the time elapsed with just weekdays? (meaning saturday and sunday will no longer be counted when measuring the difference) thanks

    CPalliniC P A 3 Replies Last reply
    0
    • Z zoroyster

      Hi I'm creating a C# program to measure differences between dates. Ive figured out how to measure the difference between two dates using DateTime and TimeSpan, but is there any short cut way on how I will be able to measure the time elapsed with just weekdays? (meaning saturday and sunday will no longer be counted when measuring the difference) thanks

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Sorry, I don't know any shortcut, but it is not such a daunting task, you need only to take special care about the working days of the first and last weeks (that maybe partial) and keep the 5/7 of the remaining... :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      Z 1 Reply Last reply
      0
      • CPalliniC CPallini

        Sorry, I don't know any shortcut, but it is not such a daunting task, you need only to take special care about the working days of the first and last weeks (that maybe partial) and keep the 5/7 of the remaining... :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        Z Offline
        Z Offline
        zoroyster
        wrote on last edited by
        #3

        i knew it wasn't going to be that easy.. haha thanks for the help CPallini. :P

        1 Reply Last reply
        0
        • Z zoroyster

          Hi I'm creating a C# program to measure differences between dates. Ive figured out how to measure the difference between two dates using DateTime and TimeSpan, but is there any short cut way on how I will be able to measure the time elapsed with just weekdays? (meaning saturday and sunday will no longer be counted when measuring the difference) thanks

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Well, may I humbly suggest my http://www.codeproject.com/csharp/TimeRanger.asp[^] You could use it to iterate across the dates and count only the weekdays, maybe even check each against some list of holidays you may have.

          1 Reply Last reply
          0
          • Z zoroyster

            Hi I'm creating a C# program to measure differences between dates. Ive figured out how to measure the difference between two dates using DateTime and TimeSpan, but is there any short cut way on how I will be able to measure the time elapsed with just weekdays? (meaning saturday and sunday will no longer be counted when measuring the difference) thanks

            A Offline
            A Offline
            AFSEKI
            wrote on last edited by
            #5

            public static int GetWorkingDatesBetween(DtaeTime DateFrom, DateTime DateTo) { if(DateFrom.CompareTo(DateTo) >= 0 ) throw new ArgumentException("Invalid date ranges! DateTo should be greater than DateFrom"); int totalWorkingDaysElapsed = 0; while(DateFrom.CompareTo(DateTo) != 0) { DateFrom = DateFrom.AddDays(1); if(DateFrom.DayOfWeek == DawOfWeek.Saturday || DateFrom.DayOfWeek == DawOfWeek.Sunday) continue; ++totalWorkingDaysElapsed; } retrun totalWorkinDaysElapsed; } Hope this helps...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups