【发布时间】:2015-12-22 07:00:56
【问题描述】:
我想在当前日期添加天数,我创建了代码,但它显示错误字符串未被识别为有效日期时间。我想要这样的日期格式'dd-MM-yyyy'
这是我的脚本
<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>
这是我的按钮点击代码
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");
}
【问题讨论】:
-
你在哪里得到这个错误?
-
@AlexZhukovskiy 在 TextBoxStartDate 行
-
您的代码似乎没问题,但您始终可以使用下面提到的 ParseExact 方法消除任何误解
-
@AlexZhukovskiy 亲爱的检查上面的错误
-
TextBoxStartDate.Text的值究竟是多少?你能调试并告诉我们吗?