2017-02-23 73 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」);

這就是你的整個時間部分將從日期時間獲取的方式。