Visual Studio: 如何从两个 DateTimePicker 获取天数和小时数

Visual Studio: How to get the number days and hours from two DateTimePicker

我需要你的帮助,我不知道该怎么做。

我有两个 DateTimePicker:

DateTimePicker1 = returns 一个日期

DateTimePicker2 - returns 一次

我想得到当前日期和时间两个DateTimePicker的的区别以获得天数和小时数

我不太确定你想要达到什么目的。不过,您可以按照以下描述进行操作

    static void Main(string[] args)
    {
        DateTime dpt1 = new DateTime(2019, 02, 15);
        DateTime dpt2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 10, 30, 45);

        DateTime combined = new DateTime(dpt1.Year, dpt1.Month, dpt1.Day, dpt2.Hour, dpt2.Minute, dpt2.Second);
        var diff = new DateTime(DateTime.Now.Ticks - combined.Ticks);
        var hours = diff.Hour;
    }

不过我发现你的问题有问题。 "I would like the difference from the current date & time to those two DateTimePicker's so I can get the total number of days and hours." 不幸的是,您将无法提取日期差异,因为在 dpt2 中您仅被选中 hours/minutes/seconds。 month/day/year 字段在计算中本质上是无用的,无法使用的。

如果你想专门获得时间跨度差异,你可以使用

轻松做到这一点
TimeSpan.FromTicks(DateTime.Now.Ticks - combined.Ticks);

Dim combine As Date = DateTimePicker1.Value.Date + DateTimePicker2.Value.TimeOfDay

Dim currentDate As Date = Date.Now()

Dim timeDifference As TimeSpan = combine - currentDate