Last post Jan 31, 2019 11:37 AM by PatriceSc
Jan 31, 2019 07:24 AM|sfayaz|LINK
I have a text box in which i select the date from the datepicker. I then need to validate: 1. A date is selected 2. date selected is not a future date. only todays date or any previous date. 3. Regular hrs add over time hours on that date do not exceed 20-24
I've an error "Error: String was not recognized as a valid DateTime." Can I please get some help over this as I have spent days over this.
protected void btnCreate_click(object sender, EventArgs e)
//DateTime dt = Convert.ToDateTime();
//var total_hrs = 24;
DateTime dt = DateTime.ParseExact(DateTxt.Text, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
// global.Text = "Done";
if (dt == null)
Response.Write("<script>alert('PLease select date');</script>");
// dateError.Text = "PLease select date";
else if (dt != null && dt > DateTime.Now)
// dateError.Text = "Invalid date";
else if(dt != null && dt <= DateTime.Now)
DateTime dt_e = dt.AddHours(23).AddMinutes(59).AddSeconds(59);
var n = DateTime.Compare(dt_e, DateTime.Now);
if (n == 1)
dateError.Text = "PLease select date";
<div class="form-group col-sm-1">
<asp:TextBox ID="DateTxt" placeholder="Date" CssClass="form-control" runat="server"
Jan 31, 2019 11:37 AM|PatriceSc|LINK
You don't have checked yet which value you have in DateTxt.Text so that you can understand why it is not parsed correctly ? I noticed this field is readonly ? It is kept that way ? A read only field is not posted so DateTxt.Text could be blank.
Also : from your logic it seems you want to use TryParseExact which returns a boolean telling it he conversion worked. The logic you have later is wrong as ParseExact works or throws an exception.