Reputation: 44
There will be 2 buttons of toggle.One will be up and other will be down Query is if current month is of 30 days then it should display 01 Aug 2017 to 16th Aug 2017 when i toggle up it should show 16th Aug to 30th Aug and similar should work for toggle down. But if current month is of 31 days then it should display 01 Aug 2017 to 16th Aug 2017 and 16th Aug to 31st Aug and similar should work for toggle down.This should work continous if am trying to toggle up it should display like below 1st Jan to 15th jan then 16th to 30th Jan then 1st Feb to 16th Feb like wise it should go on for toggle up and down. Code which i tried is below :
public static DateTime GetEndDate(int year, int month)
{
decimal currentmonth = System.DateTime.DaysInMonth(year, month);
decimal value = Math.Ceiling(Convert.ToDecimal(currentmonth / 2));
// DateTime updatedfinaldatevaluestart = new DateTime(year, month, day);
DateTime updatedfinaldatevalueend = new DateTime(year, month, Convert.ToInt32(value));
return updatedfinaldatevalueend;
}
public static DateTime GetStartDate(int year, int month)
{
decimal currentmonth = System.DateTime.DaysInMonth(year, month);
decimal value = Math.Ceiling(Convert.ToDecimal(currentmonth / 2));
// DateTime updatedfinaldatevaluestart = new DateTime(year, month, day);
DateTime updatedfinaldatevalueend = new DateTime(year, month, 01);
return updatedfinaldatevalueend;
}
Upvotes: 0
Views: 213
Reputation: 9155
You don't have to do all these calculations. The first half for all months will always be between the 1st and the 15th (you don't have to calculate 15th. There's no month that has like 45 days). And, the second half will always start from the 16th, and you can get the end date of the month by doing this (startDate
is the first day of the month):
var endDate = startDate.AddMonths(1).AddDays(-1);
Upvotes: 1
Reputation: 2760
I suspect there is a better way of doing what you're trying to achieve but there isn't enough information in the question to determine what that is (what is calling these methods, what is happening to the output etc) However, using the method you have posted, this (untested) code may do what you require
The bool parameter should be true when the down button is clicked and false when the up button is clicked
public static DateTime GetEndDate(int year, int month, bool firstHalfOfMonth)
{
if (firstHalfOfMonth == false)
{
return GetStartDate(year, month, false);
}
else
{
return new DateTime(year, month, System.DateTime.DaysInMonth(year, month));
}
}
public static DateTime GetStartDate(int year, int month, bool firstHalfOfMonth)
{
if (firstHalfOfMonth == false)
{
int daysInCurrentMonth = System.DateTime.DaysInMonth(year, month);
int midMonth = Convert.ToInt32(Math.Ceiling((double)daysInCurrentMonth / 2));
return new DateTime(year, month, midMonth);
}
else
{
return new DateTime(year, month, 1);
}
}
Upvotes: 0