C# .NET - get amount of months between dates

Asked By Sarah Nel
21-Apr-10 08:27 AM
Hallo,

I want to get how many times December and January appear between a start and end date.
Is this possible????
  Sathish S replied to Sarah Nel
21-Apr-10 08:47 AM
<FONT color=#0000ff size=2><FONT color=#0000ff size=2>
<P>private</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT 
color=#0000ff size=2>int</FONT></FONT><FONT size=2> monthcount(</FONT><FONT 
color=#2b91af size=2><FONT color=#2b91af size=2>DateTime</FONT></FONT><FONT 
size=2> from, </FONT><FONT color=#2b91af size=2><FONT color=#2b91af 
size=2>DateTime</FONT></FONT><FONT size=2> to, </FONT><FONT color=#0000ff 
size=2><FONT color=#0000ff size=2>int</FONT></FONT><FONT size=2
monthtocount)</P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>int</FONT></FONT><FONT size=2> monthcnt = 0;</P>
<P></P>
<P></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>while</FONT></FONT><FONT size=2> (from < to)</P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>if</FONT></FONT><FONT size=2> (from.Month == monthtocount) 
monthcnt++;</P>
<P>from = from.AddMonths(1);</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>return</FONT></FONT><FONT size=2> monthcnt;</P>
<P>}</P></FONT>
  Super Man replied to Sarah Nel
21-Apr-10 08:47 AM

   private void button1_Click(object sender, EventArgs e)

        {

            int january = 0;

            int december = 0;

            DateTime d1 = Convert.ToDateTime("01/01/2000");

            DateTime d2 =  Convert.ToDateTime("12/31/2010");

 

             

            for (int year = d1.Year; year <= d2.Year; year++)

            {

                if (year == d1.Year)

                {

                    for (int month = d1.Month; month <= 12; month++)

                    {

                        if (month == 12)

                        {

                            december++;

                        }

                        if (month == 1)

                        {

                            january++;

                        }

                    }

                }

                else if (year == d2.Year)

                {

                    for (int month = 1; month <= d2.Month; month++)

                    {

                        if (month == 12)

                        {

                            december++;

                        }

                        if (month == 1)

                        {

                            january++;

                        }

                    }

                }

                else

                {

                    for (int month = 1; month <= 12; month++)

                    {

                        if (month == 12)

                        {

                            december++;

                        }

                        if (month == 1)

                        {

                            january++;

                        }

                    }

                }

            }

 

            MessageBox.Show("december = " + december.ToString());

            MessageBox.Show("january = " +  january.ToString());

        }

 

  Sathish S replied to Sarah Nel
21-Apr-10 08:47 AM

private

 

int monthcount(DateTime from, DateTime to, int monthtocount)

 

{

 

int monthcnt = 0;

 

 

 

while (from < to)

 

{

 

if (from.Month == monthtocount) monthcnt++;

 

from = from.AddMonths(1);

}

 

return monthcnt;

 

}

  Sarah Nel replied to Sathish S
21-Apr-10 08:57 AM
Thank you guys!!! :-)
Create New Account
help
2011.How to I edit the current codes given below. protected void Timer1_Tick(object sender, EventArgs e) { DateTime d1 = DateTime.Now; / / DateTime d2 = Convert.ToDateTime(""); DateTime d2 = Convert.ToDateTime("GetSQLDate"); d2.AddDays(-1); TimeSpan t = d1 - d2; int timediff = t.Days; if (timediff = = 1 Response.Redirect("example.aspx"); } } public static DateTime GetSQLDate(string id) { string query = "SELECT date FROM examtimetable WHERE subject_id = '" + id + "'"; DateTime d = new
Two DateTime difference hi Expert DateTime t_date = Convert.ToDateTime("01-07-2011"); DateTime s_date = Convert.ToDateTime("14-07-1966"); i want DateTime result = "44-11-18" I to subtract two DateTime boject into c# What format is this > i want DateTime result = "44-11-18" this is DateTime result = "44-11-18"(yy-mm-dd) void
Difference between two datetime object and result into datetime hi expert DateTime t_date = Convert.ToDateTime("01-07-2011"); DateTime s_date = Convert.ToDateTime("14-07-1966"); i want result into Datetime like (18-11-44) "dd-mm-yy" please take me out from this problem. Hi newMonths , days ); } public Period WithYears ( int newYears ) { return new Period ( newYears , months , days ); } public static DateTime operator +( DateTime date , Period period ) { / / TODO: Implement this! } public static Period Difference ( DateTime first , DateTime
Datetime picker I have two datetimepickers in vb express 2008. I would like to find out anyone know how I can do that. Please help. You can use something like this; DateTime dt1 = Convert .ToDateTime(dateTimePicker1.Value); DateTime dt2 = Convert .ToDateTime(dateTimePicker2.Value); TimeSpan ts = dt2.Subtract(dt1); If this ts has some minus value that check the date, and not the time as well, use the .Date property of a DateTime object: DateTime.Now.Date Adam Private date1 as DateTime Private date2 as DateTime if DateTime.Compare(date1, date2) > 0 msg+ = "Date 1 is greater
without date and without showing am and pm. . . . plz help me out. . . public void diff() { DateTime intime = DateTime .Parse(textBox1.Text); DateTime outime = DateTime .Parse(textBox2.Text); DateTime lunch = DateTime .Parse(textBox4.Text); DateTime overtime = DateTime .Parse(textBox5.Text); / / Addition DateTime usedtime = (intime + new TimeSpan (lunch.Hour, lunch.Minute, lunch.Second