![](/img/trans.png)
[英]how to change the date format from mm/dd/yyyy to dd-mm-yyyy in combobox?
[英]How to Change the Date Format from MM/dd/yyyy to dd-MM-yyyy?
我想在創建代碼的當前日期中添加天數,但它顯示錯誤字符串未被識別為有效的DateTime。 我想要這樣的日期格式“ 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");
}
使用DateTime.ParseExact()從字符串中獲取日期對象為
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");
}
您還可以檢查字符串的有效日期為
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;
}
}
您可以參考日期檢查鏈接http://www.niceonecode.com/QA/DotNet/CSharp/how-to-check-valid-date-in-c/20271
您的代碼似乎正確,但是您的TextBoxStartDate格式不正確。
嘗試使用
DateTime myDate = DateTime.ParseExact(TextBoxStartDate.Text, "dd-MM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);
int numVal = Int32.Parse(TextBoxPredictDays.Text);
myDate.AddDays(numVal);
並確保您輸入的格式(dd-MM-yyyy)適合!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.