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. DateTime Range

DateTime Range

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 2 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.
  • L Offline
    L Offline
    lior654 hotmail com
    wrote on last edited by
    #1

    I have 2 DateTime Vars. DateTime dtFrom = '1/1/2005'; DateTime dtTo = '1/24/2005'; How can I select a period of: Week, 2 Weeks, Month... And Divide the date range into it to something like this: Example: 1 week: 1 2 3 4 5 ======== ======== ========= ========= ========= 1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005 1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005 ======== ======== ========= ========= =========

    D 1 Reply Last reply
    0
    • L lior654 hotmail com

      I have 2 DateTime Vars. DateTime dtFrom = '1/1/2005'; DateTime dtTo = '1/24/2005'; How can I select a period of: Week, 2 Weeks, Month... And Divide the date range into it to something like this: Example: 1 week: 1 2 3 4 5 ======== ======== ========= ========= ========= 1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005 1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005 ======== ======== ========= ========= =========

      D Offline
      D Offline
      dnewmon
      wrote on last edited by
      #2

      First Row Differences:
      1-1-05, 1-2-05 = 1 Day
      1-2-05, 1-9-05 = 7 Days
      1-9-05, 1-16-05 = 7 Days
      1-16-05, 1-23-05 = 7 Days

      Second Row Differences:
      1-1-05, 1-8-05 = 7 Days
      1-8-05, 1-15-05 = 7 Days
      1-15-05, 1-22-05 = 7 Days
      1-22-05, 1-24-05 = 2 Days

      I don't see any patterns in the differences here, so what exactly do you want again? What do you mean by "divide the date range into it" ? What do you want to do? Are looking for every Sunday/Saturday in the month? The work-week days? Display a calender to the screen? David

      L 1 Reply Last reply
      0
      • D dnewmon

        First Row Differences:
        1-1-05, 1-2-05 = 1 Day
        1-2-05, 1-9-05 = 7 Days
        1-9-05, 1-16-05 = 7 Days
        1-16-05, 1-23-05 = 7 Days

        Second Row Differences:
        1-1-05, 1-8-05 = 7 Days
        1-8-05, 1-15-05 = 7 Days
        1-15-05, 1-22-05 = 7 Days
        1-22-05, 1-24-05 = 2 Days

        I don't see any patterns in the differences here, so what exactly do you want again? What do you mean by "divide the date range into it" ? What do you want to do? Are looking for every Sunday/Saturday in the month? The work-week days? Display a calender to the screen? David

        L Offline
        L Offline
        lior654 hotmail com
        wrote on last edited by
        #3

        you were reading this wrong. 1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week) 1/2/2005 - 1/8/2005 - 2 (full second week) ...and so on... got it?

        D 1 Reply Last reply
        0
        • L lior654 hotmail com

          you were reading this wrong. 1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week) 1/2/2005 - 1/8/2005 - 2 (full second week) ...and so on... got it?

          D Offline
          D Offline
          dnewmon
          wrote on last edited by
          #4

          Ok, well you could try something like:

          DateTime dtStart = new DateTime(2005, 1, 1);
          DateTime dtEnd = new DateTime(2005, 1, 30);
          DateTime dtCurrent = dtStart;

          while (dtCurrent < dtEnd)
          {
          double dDays;
          if (dtCurrent.DayOfWeek != DayOfWeek.Sunday)
          dDays = 6.0 - (double)dtCurrent.DayOfWeek;
          else if ((dtCurrent.Day + 6) <= dtEnd.Day)
          dDays = 6.0;
          else
          dDays = dtEnd.Day - dtCurrent.Day;

          Console.Write(dtCurrent + " - ");
          DateTime dtEndOfWeek = dtCurrent.AddDays(dDays);
          Console.WriteLine(dtEndOfWeek);
          dtCurrent = dtEndOfWeek.AddDays(1.0);
          

          }

          David

          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