473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 63102
This is exactly what you're looking for:

System.DateTime.DaysInMonth(int 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.ArgumentOutOfRangeException("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************@removethispartdatascientific.com > wrote in
message news:09****************************@phx.gbl...
This is exactly what you're looking for:

System.DateTime.DaysInMonth(int 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******@hotmail.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.com>
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************@removethispartdatascientific.com > wrote in
message news:09****************************@phx.gbl...
This is exactly what you're looking for:

System.DateTime.DaysInMonth(int 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
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...
7
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
6
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...
10
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...
0
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...
3
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) ...
2
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...
7
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...
5
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...
12
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.