Extract the last day of a certain month from a certain year without using DaysInMonth

bmaria_berta19 21 Reputation points
2020-11-17T20:00:45.277+00:00

I need to create a function that extracts the last day of a certain month from a certain year. I am not allowed to use the function DaysInMonth(). The month and year should be the arguments of the function which returns the day. I must also use only linear structure. Has anyone got any idea?

Type of project: Windows Forms App

To show the last day, I will use a button that executes the programm and 2 textboxs in which I will type the month and year (these 2 are associated with the arguments of the defined function).

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,032 questions
Visual Studio Setup
Visual Studio Setup
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
1,032 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2020-11-17T20:20:48.08+00:00

    If you already studied how to convert strings to numbers and how to perform basic operations with DateTime, then try this approach:

    • Build a DateTime using provided month and year, specifying 1 for day. (See constructors of DateTime).
    • Use AddMonths(1), which will move to the first day of the next month.
    • Use AddDays(-1), which will give the last day of the month.

    Do you need additional assistance?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.