473,809 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get number of days in a Month

Hi,

is there anyway to get the Number of days in a given month and Year in C#

thanks

Ronald Celis
Nov 15 '05 #1
4 63154
This is exactly what you're looking for:

System.DateTime .DaysInMonth(in t year, int month)

JER
-----Original Message-----
Hi,

is there anyway to get the Number of days in a given month and Year in C#
thanks

Ronald Celis
.

Nov 15 '05 #2
Wow, I didn't know that was there. ARGH.

I guess I can throw this messy thing out:

public static int GetDaysInMonth( int month, int year)
{
if (month < 1 || month > 12)
{
throw new System.Argument OutOfRangeExcep tion("month", month, "month must
be between 1 and 12");
}
if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month ||
10 == month || 12 == month)
{
return 31;
}
else if (2 == month)
{
// Check for leap year
if (0 == (year % 4))
{
// If date is divisible by 400, it's a leap year.
// Otherwise, if it's divisible by 100 it's not.
if (0 == (year % 400))
{
return 29;
}
else if (0 == (year % 100))
{
return 28;
}

// Divisible by 4 but not by 100 or 400
// so it leaps
return 29;
}
// Not a leap year
return 28;
}
return 30;
}
Pete
"Jerry Negrelli" <je************ @removethispart datascientific. com> wrote in
message news:09******** *************** *****@phx.gbl.. .
This is exactly what you're looking for:

System.DateTime .DaysInMonth(in t year, int month)

JER
-----Original Message-----
Hi,

is there anyway to get the Number of days in a given

month and Year in C#

thanks

Ronald Celis
.

Nov 15 '05 #3
Pete Davis <pd******@hotma il.com> wrote:
Wow, I didn't know that was there. ARGH.

I guess I can throw this messy thing out:
<snip>
else if (2 == month)


<snip>

You can also throw out the above "messy" syntax, assuming you don't
actually prefer it in terms of readability to:

if (month==2)

which I believe most people do. Unlike C++, it's safe to do the above
as a typo of

if (month=2)

is caught by the compiler, as the type of the expression in an if
statement must be boolean.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thanks for the Help

"Jerry Negrelli" <je************ @removethispart datascientific. com> wrote in
message news:09******** *************** *****@phx.gbl.. .
This is exactly what you're looking for:

System.DateTime .DaysInMonth(in t year, int month)

JER
-----Original Message-----
Hi,

is there anyway to get the Number of days in a given

month and Year in C#

thanks

Ronald Celis
.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2528
by: josepe | last post by:
Hello, I need know the week nuber of the month. I know the function datepart() but returns the week number of the year a number between (1,53) I need a number between (1,4 o5). There is a function or method that i can use to do it? Thanks a lot.
7
16244
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
6
8994
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference between date and datetime classes? Please, help
10
7340
by: Bill | last post by:
Hello -- I'm parsing the output of the finger command, and was wondering something...If I'm given a month abbrievation (such as "Jan"), what's the best way to figure out the month number? I see that there's something called "month_abbr" in the calendar module. However, when I try to do calendar.month_abbr.index("Jan"), I get "_localized_month instance has no attribute 'index'." So it seems that month_abbr isn't a regular list. I'm...
0
1906
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data dates only, this code is intended to use more thrifty event descriptions (3rd saturday, last tuesday, etc.) as well as traditional one-of dates. The core here here is quite spartan, no table logic or or db cruft included (I figure you have our own...
3
4190
by: ri58776 | last post by:
Hi, does anyone have any idea how can we get last date of the month in SQL reportging. I am using following fomula, but it works only for 31 days of month, not for feb,april,june...(30 days) =month(today())& "/" & DatePart("d", DateAdd("d", -1, DateAdd("m", 1, DateAdd("d", -DatePart("d", today()) + 1, today())))) & "/" & year(today())
2
26573
by: CindySue | last post by:
Hello--I've got a query that uses DatePart("m",) to select records for a particular month, and then a report based on that query. I'd like to be able to create a field in the report that says what month they are for. I can get the month number by using =DatePart("m",) in a text box, but I can't find a way to convert it to the month name. I've experimented with a couple of things I found in the forum, but I'm not talented enough to get them to...
7
1816
by: burnside | last post by:
Hi - long time viewer, first time poster i have a car booking system that stores the startdate and the enddate of a booking as well as car/customer details. i want to produce an efficiency report that shows how many days the car is booked per month. i am having trouble getting the results - the column that sums the days between the booking start and end date works ok if the start and enddate are in the the same month, but i cant figure...
5
14462
by: girl23 | last post by:
I am using a function prototype to convert month number to month name. basically you ask the compiler to enter int from 1 to 12 and then convert it. printMonth ( number ); is the prototype and using switch this is what i did case 'm': printf( "What month? Enter a number between 1 and 12: " ); scanf( "%d%c", &number, &tmp ); printMonth( number ); break;
12
23225
by: Be Borth | last post by:
I saw previous solutions to convert a month number (1) to a month name (January). I have a database with 200+ dates. In a query, I use the "Part" function DatePart("m",), to extract the month (number) and all month extracts worked correctly (they returned the correct numerical month). However, when I attempted adding a field in the query to format that extracted month to a month name, it only returned January and December - for all 200+...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.