How to convert the “time” from DateTime into int?

2019-07-14 21:33发布

I have a DataGrid which contains a few values that are in hours and I wanted to know:

How to get ONLY the time from my DataGrid and convert it into an int (or double) variable.

My goal is to do a few operations with my DataGrid time values, like to add numbers into it

EXAMPLE: Using my "dataGridView1.Rows[1].Cells[2].Value.ToString();" It'll show a DateTime value (which is inside my DataGrid), with this value, I wanna filter ONLY the time from this and convert it into an int

the part of my code which I wanna "capture" the time:

txtAtiv.Text = dataGridView1.Rows[0].Cells[1].Value.ToString(); 
string value = dataGridView1.Rows[0].Cells[2].Value.ToString(); 
lblLeft.Text = value.Split(' ')[1]; 

I wanna get the "value" (which is a DateTime value from the DataGrid) and convert it into an int.

note: - The date for me in my dataGrid it's not relevant, I only have to pick the time (and yes, I know that I can't "split" DateTime to do them separately)

3条回答
Rolldiameter
2楼-- · 2019-07-14 22:26

First step is to convert string to DateTime. Use DateTime.TryParse(string value, out DateTime dt). Then as Mathew Watson rightly suggested, get the value of variable dt converted to milliseconds using dt.TimeOfDay.TotalMilliseconds. It is also possible to convert the span in TotalSeconds or TotalMinutes if it suits your requirement. Try to avoid calling ToString() method directly before checking if cell value is null. If I want to avoid the check, I would make compiler to do it by using something like : Rows[3].Cells[2].Value + "" instead of Value.ToString().

查看更多
女痞
3楼-- · 2019-07-14 22:37

If you are willing to be limited to millisecond resolution, then this is fairly easy.

Given a date/time that you want to get the time part from as an int, you can get the number of milliseconds since midnight, like so:

DateTime dateTime = DateTime.Now;
int timeMsSinceMidnight = (int)dateTime.TimeOfDay.TotalMilliseconds;

If you want to reconstitute the original date and time from this, you need the original date and the time since midnight in milliseconds:

DateTime date = dateTime.Date; // Midnight.
DateTime restoredTime = date.AddMilliseconds(timeMsSinceMidnight);

Test program:

DateTime dateTime = DateTime.Now;

Console.WriteLine("Original date/time: " + dateTime );

int timeMsSinceMidnight = (int)dateTime.TimeOfDay.TotalMilliseconds;

DateTime date = dateTime.Date; // Midnight.
DateTime restoredTime = date.AddMilliseconds(timeMsSinceMidnight);

Console.WriteLine("Restored date/time: " + restoredTime);

The value returned from time.TimeOfDay is of type TimeSpan, which is convenient for storing time-of-day values.

If you want to turn your "milliseconds since midnight" back into a TimeSpan, you just do this:

var timeSpan = TimeSpan.FromMilliseconds(timeMsSinceMidnight);
查看更多
Bombasti
4楼-- · 2019-07-14 22:37

Mixing Mathew's and Mukesh Adhvaryu's answers, I got into this one, and it fits perfectly on what I need, thank you guys for your support!

 txtAtiv.Text = dataGridView1.Rows[0].Cells[1].Value + "";

        string value = dataGridView1.Rows[0].Cells[2].Value + "";
        lblLeft.Text = value.Split(' ')[1];
        textStatus.Text = "";



        DateTime timeConvert;
        DateTime.TryParse(value, out timeConvert);

        double time;
        time = timeConvert.TimeOfDay.TotalMilliseconds;


        var timeSpan = TimeSpan.FromMilliseconds(time);


        lblSoma.Text = timeSpan.ToString();
查看更多
登录 后发表回答