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.
}
Related
i have dgvData(datagridview), cmbPickRoom(combobox), numDay_In & numDay_Out(numericupdown) and code which like this
private void dgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvData.Rows[e.RowIndex];
cmbPickRoom.Text = row.Cells["Room"].Value.ToString();
numDay_In.Text = row.Cells["Day_In"].Value.ToString();
numDay_Out.Text = row.Cells["Day_Out"].Value.ToString();
}
}
while the data in mysql store date format(dd-mm-yyyy) for both "Day_In" and "Day_Out".
I want when i click dgvData, numDay_In and numDay_Out only take the day(dd)
If the database fields are of type DateTime, (Day_In and Day_Out) then you don't need to convert these values to string but to a DateTime variable, then getting the day is just a matter of reading a property
DateTime inDate = Convert.ToDateTime(row.Cells["Day_In"].Value);
numDay_In.Text = inDate.Day.ToString();
You can use DateTime.TryParse to convert the user entered string to date time format and then get the Day from it as I have done in the below code.
DateTime dt = new DateTime();
if(DateTime.TryParse(row.Cells["Day_In"].Value, out dt))
{
numDay_In.Text = dt.Day.ToString();
}
else
{
//Code to display error message
}
The added advantage of this code is that you can check if the user entered format is coorect, by checking the success of the TryParse in an if condition, If it fails, you can prompt the user to enter the Date in the correct format.
I need to compare the chosen date with the date in a database and return values within the date range but keep getting incorrect format. The date format in the database is 2016-01-02 00:00:00.000 (from one of the rows)
I am using the code below but am unable to compare and keep getting"String was not recognized as a valid DateTime." Help?
protected void RadPushButton1_Click(object sender, EventArgs e)
{
DateTime startDate = DateTime.Parse(RadDatePickerStart.SelectedDate.ToString());
string s = startDate.Date.ToString("yyyy-dd-MM");
startDate = DateTime.Parse(s+"12:00:00 AM");
// DateTime endDate = DateTime.Parse(RadDatePickerEnd.SelectedDate.ToString());
EMCSettlementManager man = new EMCSettlementManager();
Grid.DataSource = man.GetSTL010(r => EntityFunctions.TruncateTime( r.settlement_date) >= startDate);
Grid.DataBind();
}
If you want to compare dates, make sure all values are of type DateTime, not String.
Try something like this:
DateTime startDate = RadDatePickerStart.SelectedDate;
EMCSettlementManager man = new EMCSettlementManager();
Grid.DataSource = man.GetSTL010(r => r.settlement_date >= startDate);
If you want to get rid of the time-part, you could do something like this:
DateTime startDate = RadDatePickerStart.SelectedDate.Date;
If the field in the database is a string-type, not a datetime, please convert it to a Date(Time)-type. Using strings everywhere is a bad choise, alsnog known as 'stringly typed'. (See: https://blog.codinghorror.com/new-programming-jargon/)
I'm using DateTimePicker control and the following code which shows a present DateTime when I select it from the picker. I need that whatever DateTime I've selected it'll shows that only, not the present.
I am displaying it on the next page...
DateTime DateFrom = DateTime.Now;
DateTime DateTo = DateTime.now;
DateTime.TryParse(datetimepicker.Value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateFrom);
DateTime.TryParse(datetimepicker.Value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTo);
Your Question is very unclear but it is clear that your not very familiar with how .net works
so here are a few examples that might highlight what you are misunderstanding
how to set the date of a picker
datetimepicker.Value = yourDateTime
how to get the date from a picker
yourDateTime = datetimepicker.Value
how to set the date range that is valid for selection
datetimepicker.MaxDate = yourMaxDate;
datetimepicker.MinDate = yourMinDate;
how to turn a string into a date
DateTime tmp;
if(DateTime.TryParse(yourString, out tmp))
yourdatetime = tmp
How to display a non editable date
label.Text = yourDateTime.ToShortDateString()
how to select a date range, either use 2 DateTimePickers or a MonthCalendar
How to remove a Timezone from the date
yourUTCDate = yourDate.ToUniversalTime()
Try this one:
DateTime DateFrom = DateTime.Now;
DateTime DateTo = DateTime.Now;
DateTime.TryParse(datetimepicker1.Value.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateFrom);
DateTime.TryParse(datetimepicker2.Value.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTo);
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
I have a program that has synchronization. That means I need to save the last synchronization date and check if it needs to be synchronized.
So, I have this:
IS.SaveContactsRetrieveDate(DateTime.Now.ToString("dd.MM.yyyy"));
Saving a date to Isolated Storage.
Then, when I call IF:
DateTime toDate = DateTime.Now;
string contactsRetriveDate = IS.ReadContactsRetriveDate();
if (contactsRetriveDate == "" || DateTime.Compare(toDate, DateTime.Parse(contactsRetriveDate)) == 1)
{
MessageBox.SHow("");
}
The problem is that when user changes the region code fails here:
DateTime.Compare(toDate, DateTime.Parse(contactsRetriveDate))
With incorrect input error.
I understand that Latvian format is dd.MM.yyyy and USA MM/dd/yyyy - but I can't find a solution...
I need all datetime parsed in one format, so I could add days, weeks and compare date.
You should serialize and deserialize your date in a culture-independent manner (where "d" is the "Short date pattern" of the Standard Date and Time Format Strings):
var s = DateTime.Now.ToString("d", CultureInfo.InvariantCulture);
var d = DateTime.Parse(s, CultureInfo.InvariantCulture);
You can use ParseExact
DateTime.ParseExact(datestring, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
you already know format so you can go for this, but make sure the string is in same format and never changes.
u can try this one:
DateTime toDate = DateTime.Now;
string contactsRetriveDate = IS.ReadContactsRetriveDate();
DateTime contactsRetriveDat = Convert.ToDateTime(contactsRetriveDate);
if (contactsRetriveDate == "" || toDate.CompareTo(contactsRetriveDat)==0)
{
MessageBox.SHow("");
}