2017-02-23 72 views
0

我正在使用Windows窗体中的项目,在这里我必须将日期从Excel文件转移到DataGridView中。如何从“时间”类型值分钟

我做到了成功,但在Excel中的一个细胞是一类的“时间”,并有书面次如"11:35:19 AM""12:56:15 PM" ...

在DataGridView中,它只是复制值,但我需要知道分钟或小时。有了这个代码,我可以得到这个单元格的值:

dataGridView2.Rows[i].Cells[2].Value 

但它给我{12/30/1899 12:49:40 PM}。这里的时间是正确的,但它给了我额外的日期(12/30/1899它不是写在Excel文件中)。

那么如何从这个值只获得分钟,小时或秒?在DataGridView中,我必须添加一个新的Cell并在那里写入Hour。例如:

12:19:20 | 12 
10:29:45 | 10 
11:39:50 | 11 
13:49:10 | 13 
+0

有点格式化可以让问题变得更容易阅读。花点时间熟悉SO的格式化/降价。 – spender

+0

'int minutes =((DateTime)dataGridView2.Rows [i] .Cells [2] .Value).Minute;' – Innat3

回答

0
DateTime dt = DateTime.Parse(dataGridView2.Rows[i].Cells[2].Value); 
dt.hours; // hours 
dt.minutes; // minutes 
dt.second; // seconds 
+0

非常感谢 –

+0

非常欢迎 –

0

串时间=((日期时间)dataGridView2 .Rows [i] .Cells [2] .Value).ToString(“hh:mm tt”);

这就是你的整个时间部分将从日期时间获取的方式。