I'm using Calendar controller in my asp.net web forms application. I followed this article to implement Calendar in my application. I'm adding selected days into a List<DateTime> to remember selected dates and use them in future actions.
Now I have added buttons to my page like Select Weekends, Select Weekdays, Select Month and Select Year.
if I click on the Select Weekends button, I need to select all weekend days of current month and add them into List<DateTime>.
if I click on the Select Weekdays button, I need to select all week days of current month and add them into List<DateTime>.
if I click on the Select Month** button, I need to select all days of the current month and add them into List<DateTime>.
if I click on the Select Year button, I need to select all days of the current year and add them into List<DateTime>.
How can I do this programatically using C#?
I don't think there's a miracle solution, here how I would write 2 methods to what you want for the weekend days. For the other points, you could do more or less the same thing:
protected void WeekendDays_Button_Click(object sender, EventArgs e)
{
this.SelectWeekEnds():
}
private void SelectWeekEnds(){
//If you need to get the selected date from calendar
//DateTime dt = this.Calendar1.SelectedDate;
//If you need to get the current date from today
DateTime dt = DateTime.Now;
List<DateTime> weekendDays = this.SelectedWeekEnds(dt);
weekendDays.ForEach(d => this.Calendar1.SelectedDates.Add(d));
}
private List<DateTime> GetWeekEndDays(DateTime DT){
List<DateTime> result = new List<DateTime>();
int month = DT.Month;
DT = DT.AddDays(-DT.Day+1);//Sets DT to first day of month
//Sets DT to the first week-end day of the month;
if(DT.DayOfWeek != DayOfWeek.Sunday)
while (DT.DayOfWeek != DayOfWeek.Saturday)
DT = DT.AddDays(1);
//Adds the week-end day and stops when next month is reached.
while (DT.Month == month)
{
result.Add(DT);
DT = DT.AddDays(DT.DayOfWeek == DayOfWeek.Saturday ? 1 : 6);
}
return result;
}
Related
I'm trying to Get the First and Last Day of 4 Weeks based on the given Date.
For Example:
If the given Date is 8-Jan-2023 then the week start and end should be 1-Jan-2023 and 28-Jan-2023 respectively.
If the given Date is 10-Jan-2023 then the week start and end should be 1-Jan-2023 and 28-Jan-2023 respectively.
If the given Date is 30-Jan-2023 then the week start and end should be 29-Jan-2023 and 25-Jan-2023 respectively.
Basically, the use case depends on the very first input. If the first input is 15-Jan then the first 4 weeks are recorded in the database as 15-Jan to 11-Feb. In that case, 17-Jan belongs to that week
How to do this?
I'm getting the Start Day & end Date of the week as below:
public static DateTime FirstDayOfWeek(this DateTime dt)
{
var culture = Thread.CurrentThread.CurrentCulture;
var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-diff).Date;
}
public static DateTime LastDayOfFourWeeks(this DateTime dt) =>
dt.FirstDayOfWeek().AddDays(27);
before getting started I want to tell that I already know how to select the 1st and last date of the current month. But, my question is different, I want to select the 1st and the last date of the selected month using date-time picker in c# winforms.
public Form1()
{
InitializeComponent();
MonthYearDT();
}
private string MonthYearDT()
{
dTMY.Format = DateTimePickerFormat.Custom;
dTMY.CustomFormat = "yyyy-MM-dd";
dTMY.ShowUpDown = true; // to prevent the calendar from being displayed
return dTMY.Value.ToString("yyyy-MM-dd");
}
private void BtnExecute_Click(object sender, EventArgs e)
{
.
.
.
.
monthYear = MonthYearDT();
.
.
.
}
In the above, the value of monthYear is 2019-12-19. It can be changed to any month. I want to get the 1st 2019-12-01 and last date 2019-12-31 of the selected month.
How can I do this?
Any help would be highly appreciated.
Before doing anything firstly change the function MonthYearDT() from string to DateTime
private DateTime MonthYearDT()
{
dTMY.Format = DateTimePickerFormat.Custom;
dTMY.CustomFormat = "yyyy-MM";
dTMY.ShowUpDown = true; // to prevent the calendar from being displayed
dTMY.Enabled = false;
return dTMY.Value;
}
Getting the first is easy. Just create a new DateTime like so
DateTime selectedDate = MonthYearDT();
var first = new DateTime(selectedDate.Year, selectedDate.Month, 1);
getting the second is just as easy. Use first, add one month and subtract one day like so
var second = first.AddMonths(1).AddDays(-1);
firstDate = first.ToString("yyyy-MM-dd");
lastDate = second.ToString("yyyy-MM-dd");
Universal truth is that the first day of any month would always be same.
And to get the last date below DateTime function can be used. You need year also along the month to get the last day
var lastDay = DateTime.DaysInMonth(year, month);
var lastDayDate = new DateTime(year, month, lastDay);
I have something odd requirement. I have Current Date and List of Week Days. And I want next all possible date till the target date.
For i.e. Today, its 22-04-2014 And Tuesday. Target date is 15-05-2014I have 2 week days, Monday and Thursday. So code should find near by Week Day, which will be Thursday here. So It should return date of Thursday which is 24-04-2014. Now, next turn is of Monday which comes from List. So now, It should return date of Monday which is 28-04-2014.
It should keep repeating till the target date.
So, final result will be
24-04-2014,
28-04-2014,
1-05-2014,
5-05-2014,
8-05-2014,
12-05-2014
Please help me to get this type of result. Here, Monday and Thursday is not fixed. It can be any Day and any number of Day.
Update : Link to the working example - Example
You can try this code, i have tested it and working correctly
private List<DateTime> ProcessDate(DateTime dtStartDate, DateTime targetDate)
{
DateTime dtLoop = dtStartDate;
//dtRequiredDates to hold required dates
List<DateTime> dtRequiredDates = new List<DateTime>();
for (int i = dtStartDate.DayOfYear; i < targetDate.DayOfYear; i++)
{
if (dtLoop.DayOfWeek == DayOfWeek.Monday || dtLoop.DayOfWeek == DayOfWeek.Thursday)
{
dtRequiredDates.Add(dtLoop);
}
dtLoop = dtLoop.AddDays(1);
}
return dtRequiredDates;
}
You may have to enhance this codes so that it doesn't throw any exception based on the requirement.
UPDATE 2:
You can have another method which will accept the days of week as follows
private List<DateTime> ProcessDate(DateTime dtStartDate, DateTime targetDate, List<DayOfWeek> daysOfWeek)
{
DateTime dtLoop = dtStartDate;
List<DateTime> dtRequiredDates = new List<DateTime>();
for (int i = dtStartDate.DayOfYear; i < targetDate.DayOfYear; i++)
{
foreach (DayOfWeek day in daysOfWeek)
{
if (dtLoop.DayOfWeek == day)
{
dtRequiredDates.Add(dtLoop);
}
}
dtLoop = dtLoop.AddDays(1);
}
return dtRequiredDates;
}
Here is the Example
Hence you can pass any number of week days as you wish.
Hope this helps
You could try something like this:
List<DayOfWeek> listOfDays = new List<DayOfWeek>{DayOfWeek.Monday, DayOfWeek.Thursday};
var end = new DateTime(2014,05,15);
var day = DateTime.Now.Date;
while (day < end)
{
day.AddDays(1); // adds +1 days to "day"
if (listOfDays.Contains(day.DayOfWeek)) Console.WriteLine(day.Date.ToString());
}
(I can't test the code right now, so maybe you need to modify a little ;-)
The following code displays a list of appointments scheduled for a specific date in my windows phone 7 app
private void DisplayAppointmentsForDate(DateTime date)
{
appointmentsSource.FetchData(date, date.AddDays(1));
this.AppointmentsList.ItemsSource = appointmentsSource.GetAppointments((IAppointment appointment) =>
{
DateTime currentAppointmentStart = appointment.StartDate;
DateTime currentAppointmentEnd = appointment.EndDate;
DateTime requiredAppointmentsStartDate = date.Date;
DateTime requiredAppointmentsEndDate = date.Date.AddDays(1);
if (requiredAppointmentsEndDate > currentAppointmentStart && requiredAppointmentsStartDate < currentAppointmentEnd)
{
return true;
}
return false;
});
}
private void RadCalendar_SelectedValueChanged(object sender, ValueChangedEventArgs<object> e)
{
if (e.NewValue == null)
{
this.AppointmentsList.ItemsSource = null;
return;
}
DisplayAppointmentsForDate((e.NewValue as DateTime?).Value);
}
But this shows the appointments scheduled for a specific date. EG. If 23/09/2013 is selected on the calendar, then the list would display all the appointments on that day.
I want to change this so that the list displays all appointments for a particular month. EG. If the calendar is on the month of July 2013, the the list should show all appointments in that month.
The Start and End Dates are all set to DateTime type with values such as 23/09/2013 12:01PM, and this is how it is used to get list of apps.
I've tried using appointmentsSource.FetchData(date.Month, date.AddDays(1));, but it doesn't work.
How do I do this?
I'm assuming that the method FetchData takes a "start date" parameter and an "end date parameter", in which case, you just need to get the first date of the current month and the last date of the current month
var currentDate = DateTime.Now; // Or whatever date you want to displa
var startDate = new DateTime(currentDate.Year, currentDate.Month, 1);
var endDate = new DateTime(currentDate.Year, currentDate.Month, DateTime.DaysInMonth(currentDate.Year, currentDate.Month));
appointmentsSource.FetchData(startDate, endDate);
if your FetchData takes a month parameter and the number of days, then
appointmentsSource.FetchData(currentDate.Month, DateTime.DaysInMonth(currentDate.Year, currentDate.Month));
we really need to see the "FetchData" method to be able to provide you with a definitive answer
how could i programmatically detect, how many days are there for a particular month & year.
It's already there:
DateTime.DaysInMonth(int year, int month);
should do it.
Something like this should do what you want:
static int GetDaysInMonth(int year, int month) {
DateTime dt1 = new DateTime(year, month, 1);
DateTime dt2 = dt1.AddMonths(1);
TimeSpan ts = dt2 - dt1;
return (int)ts.TotalDays;
}
You get the first day of the month, add one month and count the days in between.