I want to add the days in current date i have created the code but it showing error String was not recognized as a valid DateTime. I want the date format like this 'dd-MM-yyyy'
Here is my Script
<script>
function addDate() {
debugger;
//Get the entered datevalue
var enteredDateVal = new moment(document.getElementById("TextBoxStartDate").value);
//Get the days to add
var numberofDays = document.getElementById("TextBoxPredictDays").value
//Add the days using add method in moment.js
enteredDateVal.add("days", parseInt(numberofDays));
//Assign the value in textbox
document.getElementById("TextBoxPredictedClosing").value = enteredDateVal.format("dd-MM-yyyy");
}
</script>
and here is my Code Behind for Button Click
protected void Button9_Click(object sender, EventArgs e)
{
// this.TextBoxStartDate ="dd-MM-yyyy";
DateTime dtval = DateTime.Parse(TextBoxStartDate.Text);
//Add values here
DateTime formatteddays = dtval.AddDays(Int16.Parse(TextBoxPredictDays.Text));
TextBoxPredictedClosing.Text = formatteddays.ToString("dd-MM-yyyy");
}
I am updating with error for All of you dear and thanks for response kindly help here is the Error
Use DateTime.ParseExact() to get date object from string as
protected void Button9_Click(object sender, EventArgs e)
{
// this.TextBoxStartDate ="dd-MM-yyyy";
DateTime dtval = DateTime.ParseExact(TextBoxStartDate.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
//Add values here
DateTime formatteddays = dtval.AddDays(Int16.Parse(TextBoxPredictDays.Text));
TextBoxPredictedClosing.Text = formatteddays.ToString("dd-MM-yyyy");
}
You can also check string for valid date as
public static bool IsDate(string tempDate)
{
DateTime fromDateValue;
var formats = new[] { "dd-MM-yyyy" };
if (DateTime.TryParseExact(tempDate, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out fromDateValue))
{
return true;
}
else
{
return false;
}
}
You can refer to link for Date check http://www.niceonecode.com/QA/DotNet/CSharp/how-to-check-valid-date-in-c/20271
Your code seems to be right but your TextBoxStartDate is not in the correct format.
Try using
DateTime myDate = DateTime.ParseExact(TextBoxStartDate.Text, "dd-MM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);
int numVal = Int32.Parse(TextBoxPredictDays.Text);
myDate.AddDays(numVal);
And make sure that your input format (dd-MM-yyyy) fits!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.