将字符串转换为日期时间[2017年3月1日星期三上午6:00] c#中的此格式

时间:2017-03-30 20:31:26

标签: c# excel datetime

我有excel表格,其中包含日期和时间列,日期格式为SELECT io_data.Timestamp, io.Name AS ioName, io_data.ReadingValue FROM io_data io WHERE io_data.Actif = 1 AND io.ID_IO_Type = 3 ORDER BY io.Name, io.ID_IO_Type, io_data.Timestamp ASC ,而时间格式为Wed Mar 01, 2017我从excel表中获取了这些值,并将这两列合并为一个空格使它6:00 AM看起来像这样。现在我想将此字符串转换为datetime。我尝试了很多东西,但是我得到的错误是字符串不是有效的日期时间格式。

Wed Mar 01, 2017 6:00 AM

我希望将此转换为相同格式的datetime,因为我有另一列告诉我这段时间也添加了几小时,我正在使用string startDate = row.Cells[0].Value.ToString(); string startTime = row.Cells[1].Value.ToString(); var mydate = startDate+ " " + startTime; // it creates Wed Mar 01, 2017 6:00 AM, this is shown in debugger DateTime finaldate = DateTime.ParseExact(mydate, "r", null); 来执行此操作。所以我想在添加小时后以相同的格式输出。 我不能使这种格式的日期格式字符串给ParseExact。任何帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:1)

您可以指定自定义格式:

ssl