在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响应,有很多简单的方法可以做到,然后手动解析我们的每个部分。 –