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. Calculate How Many Mondays in a Particular Month

Calculate How Many Mondays in a Particular Month

Scheduled Pinned Locked Moved C#
helpquestion
22 Posts 8 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.
  • M musefan

    Improved...

    int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
    {
    DateTime dt = new DateTime(year, month, 1);
    int firstInstance = 1;
    if((int)dt.DayOfWeek > (int)dayToCount)
    firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
    else if((int)dt.DayOfWeek < (int)dayToCount)
    firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
    return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
    }

    ...I think the logic is right. [EDIT] That was way off, I fixed now... I think!

    Life goes very fast. Tomorrow, today is already yesterday.

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

    What I came up with is similar to that:

    public static int
    F
    (
    int Year
    ,
    int Month
    ,
    System.DayOfWeek DayOfWeek
    )
    {
    int d = (int) DayOfWeek - (int) (new System.DateTime ( Year , Month , 1 )).DayOfWeek ;

    if ( d < 0 )
    {
        d += 7 ;
    }
    
    int n = System.DateTime.DaysInMonth ( Year , Month ) - 28 ;
    
    return ( d < n ? 5 : 4 ) ;
    

    }

    1 Reply Last reply
    0
    • L Luc Pattyn

      PIEBALDconsult wrote:

      I expect the OP's teacher only wants to prove that he's smarter than his students

      The teacher may be shaded then by a cheating student. :laugh:

      Luc Pattyn [Forum Guidelines] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      C Offline
      C Offline
      cdpace
      wrote on last edited by
      #22

      Lol guys Thank you very much for the help ;) but actually this ain't for homework hehe, this is part of a payroll system that I am developing at work and with the deadline stress and from being tired because been programming 8 hours strait i couldn't find a solution for this so i turned to some help ;) hehe thank you again guys.

      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