Calendar calculation

M

^MisterJingo^

Hi all,

Does the calendar control provide any easy way to detect the last day
of the currently displayed month? I'm using the VisibleMonthChanged
event, and can find the first day of the month using e.NewDate.
I need to find the last date day of the month to pull month specific
data from a database (Rather than pulling everything).
Any help or tips on how to calculate this would be appreciated.

Thanks.
 
R

Richard Brown

^MisterJingo^ said:
Hi all,

Does the calendar control provide any easy way to detect the last day
of the currently displayed month? I'm using the VisibleMonthChanged
event, and can find the first day of the month using e.NewDate.
I need to find the last date day of the month to pull month specific
data from a database (Rather than pulling everything).
Any help or tips on how to calculate this would be appreciated.

Thanks.

Why not make the month part of you SQL eg:
select * from TABLE where datepart(Mm,DATEFIELD) = @MONTH_VALUE and
datepart(Yy,DATEFIELD) = @YEAR_VALUE
 
J

Jared

How about using e.NewDate.DaysInMonth? You could then replace the days
element of your date with this integer, e.g.

int intDay = e.NewDate.DaysInMonth;

string strDate = e.NewDate.ToString("MM/dd/yyyy HH:mm:ss");

strDate = strDate.Substring(0,(strDate.IndexOf("/",0)) -1) + intDay +
strDate.SubString(strDate.IndexOf("/",0) + 4) ;

DateTime dteDate = Convert.ToDateTime(strDate);

I've not tried this, so it might not work and there is probably a
simpler method.

Jared
 
M

^MisterJingo^

Richard said:
Why not make the month part of you SQL eg:
select * from TABLE where datepart(Mm,DATEFIELD) = @MONTH_VALUE and
datepart(Yy,DATEFIELD) = @YEAR_VALUE

Hi Richard,

Thanks for the above. The problem is that i'm currently using MySQL as
the DB and it doesn't have a datepart function. I'm looking for
something similar in its documentation.
 
M

^MisterJingo^

Jared said:
How about using e.NewDate.DaysInMonth? You could then replace the days
element of your date with this integer, e.g.

int intDay = e.NewDate.DaysInMonth;

string strDate = e.NewDate.ToString("MM/dd/yyyy HH:mm:ss");

strDate = strDate.Substring(0,(strDate.IndexOf("/",0)) -1) + intDay +
strDate.SubString(strDate.IndexOf("/",0) + 4) ;

DateTime dteDate = Convert.ToDateTime(strDate);

I've not tried this, so it might not work and there is probably a
simpler method.

Jared

Hi Jared,

Thanks for this. It turns out e.NewDate hasn't got the DaysInMonth
method, so I did the following:

int day = DateTime.DaysInMonth(e.NewDate.Year, e.NewDate.Month);

start = e.NewDate;
end = new DateTime(e.NewDate.Year, e.NewDate.Month, day);

start and end are datetimes used the in the SQL statement
 
R

Richard Brown

^MisterJingo^ said:
Hi Richard,

Thanks for the above. The problem is that i'm currently using MySQL as
the DB and it doesn't have a datepart function. I'm looking for
something similar in its documentation.

Looks like you have this sorted from your later posts:

But if not note that mySQL appears to support MONTH(date) and
YEAR(date) functions, I've not tested this as I don't have a mySQL db
but my guess is you could achieve the above using these.
 
M

^MisterJingo^

Ok, I've get things working but I've noticed strange behaviour with
this control.

I use the following function to change a days colour if its date is in
the DB:


protected void Cal_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth)
{
foreach (DataRow dr in _ds.Tables[0].Rows)
{
if ((dr["startdate"].ToString() != DBNull.Value.ToString()))
{
Response.Write(dr["startdate"].ToString());
DateTime dtEvent = (DateTime)dr["startdate"];
if (dtEvent.Equals(e.Day.Date))
{
e.Cell.BackColor = System.Drawing.Color.PaleVioletRed;
}
}
}
}
}

The Response.Write is just used for testing. When this app is run, the
following is produced:

http://www.misterjingo.com/cal.gif

Does anyone have an idea why so many lines are being outputted? I set a
counter up to see how many times the foreach is run, and even if its
run 1 time, the same still happens. The above function is only called
once too.
 
M

^MisterJingo^

^MisterJingo^ said:
Ok, I've get things working but I've noticed strange behaviour with
this control.

I use the following function to change a days colour if its date is in
the DB:


protected void Cal_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth)
{
foreach (DataRow dr in _ds.Tables[0].Rows)
{
if ((dr["startdate"].ToString() != DBNull.Value.ToString()))
{
Response.Write(dr["startdate"].ToString());
DateTime dtEvent = (DateTime)dr["startdate"];
if (dtEvent.Equals(e.Day.Date))
{
e.Cell.BackColor = System.Drawing.Color.PaleVioletRed;
}
}
}
}
}

The Response.Write is just used for testing. When this app is run, the
following is produced:

http://www.misterjingo.com/cal.gif

Does anyone have an idea why so many lines are being outputted? I set a
counter up to see how many times the foreach is run, and even if its
run 1 time, the same still happens. The above function is only called
once too.

The number of times the Response.Write text is repeated seems to
correlate to the number of days in the month. Is this normal behaviour?
 
M

^MisterJingo^

I have one more question regarding this control. I know its possible to
change the tooltip text of a control, but is it possible to change the
'title' text of the default day number created by the control? As this
link dominates the cell, more often than not, people will see the title
text (day and month of the cell link under the mouse pointer) rather
than the tooltip text (around the link).
I've googled for a solution but cannot find anything yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top