wpf 如何将 DateTime 中的“时间”转换为 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36913659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert the "time" from DateTime into int?
提问by MattDAVM
I have a DataGrid which contains a few values that are in hours and I wanted to know:
我有一个 DataGrid,其中包含一些以小时为单位的值,我想知道:
How to get ONLY the time from my DataGrid and convert it into an int (or double) variable.
如何仅从我的 DataGrid 获取时间并将其转换为 int(或 double)变量。
My goal is to do a few operations with my DataGrid time values, like to add numbers into it
我的目标是对我的 DataGrid 时间值进行一些操作,比如向其中添加数字
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
示例:使用我的“dataGridView1.Rows[1].Cells[2].Value.ToString();” 它会显示一个 DateTime 值(它在我的 DataGrid 中),有了这个值,我只想过滤这个时间并将其转换为 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.
我想获取“值”(这是来自 DataGrid 的 DateTime 值)并将其转换为 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)
注意: - 我的 dataGrid 中的日期不相关,我只需要选择时间(是的,我知道我不能“拆分” DateTime 来分别执行它们)
采纳答案by Mukesh Adhvaryu
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().
第一步是将字符串转换为 DateTime。使用 DateTime.TryParse(string value, out DateTime dt)。然后正如 Mathew Watson 正确建议的那样,使用 dt.TimeOfDay.TotalMilliseconds 将变量 dt 的值转换为毫秒。如果满足您的要求,也可以在 TotalSeconds 或 TotalMinutes 中转换跨度。在检查单元格值是否为空之前,尽量避免直接调用 ToString() 方法。如果我想避免检查,我会让编译器通过使用类似的东西来做到这一点:Rows[3].Cells[2].Value + "" 而不是 Value.ToString()。
回答by Matthew Watson
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.TimeOfDayis of type TimeSpan, which is convenient for storing time-of-day values.
返回的值time.TimeOfDay是 类型TimeSpan,便于存储时间值。
If you want to turn your "milliseconds since midnight" back into a TimeSpan, you just do this:
如果您想将“自午夜以来的毫秒数”变回TimeSpan,只需执行以下操作:
var timeSpan = TimeSpan.FromMilliseconds(timeMsSinceMidnight);
回答by MattDAVM
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!
混合 Mathew 和 Mukesh Adhvaryu 的答案,我进入了这个,它完全符合我的需要,谢谢你们的支持!
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();
回答by Sunil Raj
static void Main(string[] args)
{
string time1 = "11:15 AM";
string time2 = "11:15 PM";
var t1 = ConvertTimeToInt(time1);
var t2 = ConvertTimeToInt(time2);
Console.WriteLine("{0}", t1);
Console.WriteLine("{0}", t2);
Console.WriteLine("{0:dd/MM/yyyy hh:mm tt}", ConvertIntToTime(t1));
Console.WriteLine("{0:dd/MM/yyyy hh:mm tt}", ConvertIntToTime(t2));
Console.ReadLine();
}
static long ConvertTimeToInt(string input)
{
var date = DateTime.ParseExact(input, "hh:mm tt", CultureInfo.InvariantCulture);
TimeSpan span = date.TimeOfDay;
Console.WriteLine("{0:dd/MM/yyyy hh:mm tt}", date);
return span.Ticks;
}
static DateTime ConvertIntToTime(long input)
{
TimeSpan span = TimeSpan.FromTicks(input);
var date = new DateTime(span.Ticks);
Console.WriteLine("{0:dd/MM/yyyy hh:mm tt}", date);
return date;
}

