Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 372 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting number of days in a month

#1
I have a comboBox with all of the months in it.

What I need to know is the number of days in the chosen month.

var month = cmbMonth.SelectedIndex + 1;
DateTime date = Convert.ToDateTime(month);

So if a user selects January, I need to save 31 to a variable.
Reply

#2
You want [`DateTime.DaysInMonth`][1]:

int days = DateTime.DaysInMonth(year, month);

Obviously it varies by year, as sometimes February has 28 days and sometimes 29. You could always pick a particular year (leap or not) if you want to "fix" it to one value or other.


[1]:

[To see links please register here]

Reply

#3
Use [System.DateTime.DaysInMonth][1], from code sample:

const int July = 7;
const int Feb = 2;

// daysInJuly gets 31.
int daysInJuly = System.DateTime.DaysInMonth(2001, July);

// daysInFeb gets 28 because the year 1998 was not a leap year.
int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);

// daysInFebLeap gets 29 because the year 1996 was a leap year.
int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);

[1]:

[To see links please register here]

Reply

#4
-

`int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);`

-----------------------------------------------------------------------

if you want to find days in this year and present month then this is best
Reply

#5


int days = DateTime.DaysInMonth(int year,int month);
or

int days=System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(int year,int month);

you have to pass year and month as <code>int</code> then days in month will be return on currespoting year and month
Reply

#6
I made it calculate days in month from datetimepicker selected month and year , and I but the code in datetimepicker1 textchanged to return the result in a textbox
with this code

private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
int s = System.DateTime.DaysInMonth(DateTimePicker1.Value.Date.Year, DateTimePicker1.Value.Date.Month);

TextBox1.Text = s.ToString();
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through