在C#/ WinForm的,我能夠將一個字符串解析爲日期,如果用戶輸入:dd/mm/yyyy
解析字符串的日期沒有斜槓
DateTime.Parse(date).ToString();
我希望能在沒有解析斜線(例如在datagridview或DateTimePicker中)。
01022012
應該解釋爲01/02/2012
任何人都知道如何與DateTime.Parse
解析呢?
這裏是我的代碼:
private void dataGridView_BadgeService_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
{
string date = Convert.ToString(e.FormattedValue).Trim();
if (date.Length > 0)
{
try
{
DateTime _date;
DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date);
date = _date.ToShortDateString();
dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = date;
}
catch
{
MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Cancel = true;
}
}
}
}
這裏是異常消息:
它說: 「System.FormatException:該字符串未被識別爲一個DateTime的Valide」
@Sander Stackoverflow之前,我找不到任何快速搜索。而[鏈接的問題](http://stackoverflow.com/questions/5793163/c-sharp-winforms-datetimepicker-custom-format)絕對不會幫助沃爾特。 –
爲什麼你的問題標記爲json,json如何被invloved?如果你想解析出一個json響應,有很多簡單的方法可以做到,然後手動解析我們的每個部分。 –