Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a string which is like "yyyymmdd.csv" i.e. "20120219.csv"
Question:
How can I find out from this string if the date is last day of the currecnt month.?
Thanks
Posted

1 solution

Get a date field into a variable using DateTime.TryParse ie. TryParse(20120219);.
Then just use the DateTime.DaysInMonth method[^] to get the number of days in this month (the month and year argument can be passed using the variable output).

e.g.
DateTime.TryParse(filename,dtLocalDate);
int daysInMonth = DateTime.DaysInMonth(dtLocalDate.Year, dtLocalDate.Month);
if (daysInMonth == dtLocalDate.Day) 
{
   //Last day in month
}
 
Share this answer
 
Comments
arkiboys 29-Feb-12 4:49am    
What do you think about this?
private bool IsLastDayOfMonth(DateTime dtDate)
{
DateTime dtTo = dtDate.Date;
dtTo = dtTo.AddMonths(1);
// remove all of the days in the next month
// to get bumped down to the last day of the
// previous month
dtTo = dtTo.AddDays(-(dtTo.Day));

if (dtDate == dtTo)
{
return true;
}
else
{
return false;
}
}
Abhinav S 29-Feb-12 7:22am    
Looks ok. Unless I'm missing something.
SteveAdey 29-Feb-12 14:54pm    
Wouldn't this be slightly easier:

private bool IsLastDayOfMonth(DateTime dt)
{
return !(dt.AddDays(1).Month == dt.Month);
}
arkiboys 29-Feb-12 7:26am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900