my value in excel cell has total time : 227:20 and this format [hh]:mm
but not convert in c# this format and show 11:20 in gridview
my code:
model.ezafekari = DateTime.FromOADate((double)ws.Cells[rowNum, 17].Value).ToString("HH:mm");
CodePudding user response:
I'm assuming that your formula:
(double)ws.Cells[rowNum, 17].Value
Will yield what Excel will show as the General
formatted value for the cell (in my experience, that's what you see for date/time cells). I'm not going to get code running against Excel.
Once you have that value, this should work:
var real = 9.472222222; // should be (double)ws.Cells[rowNum, 17].Value
var days = (int)Math.Truncate(real);
var fracDays = real - days;
var fracHours = fracDays * 24.0;
var hours = (int) Math.Truncate(fracHours);
var minutes = (int) Math.Round ((fracHours - hours) * 60);
var totalHours = (days * 24) hours;
var toShow = $"{totalHours}:{minutes}";
If you step through that code, you will see:
Variable | Value |
---|---|
days | 9 |
hours | 11 |
minutes | 20 |
totalHours | 227 |
toShow | 227:20 |