Get dates by month in C# Windows Phone 7 - c#

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

Related

Getting first and last date of selected month c# winforms

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);

Error for converting some specific date in persian calendar format

We have website that everybody can add a record witch includes date column
everytime the add a record to database a date will save in date column
and when they want to see them in a gridview, it will be convert in persian calendar format
The problem is that some specific dates can not convert.for example if we want to convert date 1398/6/31 to Gregorian date, It will give an error
protected void Page_Load(object sender, EventArgs e)
{
System.DateTime date = System.DateTime.Today;
System.Globalization.PersianCalendar p = new
System.Globalization.PersianCalendar();
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
System.DateTime currentDate = new System.DateTime(1398, 6, 31);
azsh.Text = currentDate.ToString("d/M/yyyy");
}
Year, Month, and Day parameters describe an unrepresentable DateTime.

Get Dates based on Current Date and List of Week Days

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 ;-)

Select dates in asp.net Calendar programatically

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;
}

Prevent user from entering a from date greater than to date c#

I am searching data based on two dates a from field and a To field in Asp.net
I want to prevent the user from entering a From date greater than a To date and display a message to the user Please select a valid date range
DateTime InvoiceDateFrom = new DateTime();
DateTime InvoiceDateTo = new DateTime();
if (TxtInvoiceDateFrom.Text.Trim() != "")
{
//DateTime FromDate = DateTime.ParseExact(TxtInvoiceDateFrom.Text.Trim(), "dd/MM/yyyy", null).AddDays(1);
InvoiceDateFrom = Convert.ToDateTime(TxtInvoiceDateFrom.Text);
//DateTime toDate = DateTime.ParseExact(TxtInvoiceDateTo.Text.Trim(), "dd/MM/yyyy", null).AddDays(1);
}
if (TxtInvoiceDateTo.Text.Trim() != "")
{
InvoiceDateTo = Convert.ToDateTime(TxtInvoiceDateTo.Text);
}
if (InvoiceDateTo < InvoiceDateFrom)
MessageBox.Show("Please select a valid date range.");
DateTime x = DateTime.Parse("12/8/2012"); //as "12/8/2012" is the your specified date
dateTimePicker1.MaxDate = x; // or you can use it in one line
if you want to prevent the user to choose date greater than today:
dateTimePicker1.MaxDate = DateTime.Today;
Please see if this is of help! C# way of doing is fine. But i would rather suggest javascript.
DateTime toDate=DateTime.ParseExact(todateString,"dd/MM/yy",System.Globalization.InvariantCulture);
DateTime fromDate=DateTime.ParseExact(fromdateString,"dd/MM/yy",System.Globalization.InvariantCulture);
int comparison=DateTime.Compare(toDate,fromDate);
if(comparison>=0)
{
//Post custom error message.
}

Categories