C# 计算从当前时间到特定时间的毫秒数

C# 计算从当前时间到特定时间的毫秒数,c#,visual-studio,timer,C#,Visual Studio,Timer,所以我有一个字符串currentTask.time,它存储了一个类似于“13:00”的值 有没有一种方法可以根据当前系统时间来计算,也就是到该时间的毫秒数 所以基本上,假设时间是12:59,如果我调用这个函数通过'13:00',它应该返回60000 我不确定最好的方法是什么 提前谢谢 首先,将其转换为日期时间: //note: you can also use ParseExact if you need a specific format. var dt = DateTime.Parse(cu

所以我有一个字符串
currentTask.time
,它存储了一个类似于“13:00”的值

有没有一种方法可以根据当前系统时间来计算,也就是到该时间的毫秒数

所以基本上,假设时间是12:59,如果我调用这个函数通过'13:00',它应该返回60000

我不确定最好的方法是什么


提前谢谢

首先,将其转换为日期时间:

//note: you can also use ParseExact if you need a specific format.
var dt = DateTime.Parse(currentTask.time);
然后,从当前时间中减去它,结果是

您的结果位于
totalmillizes
属性中。见:

请注意,这有一个约3-6毫秒的侵入。如果你需要更多的精度,请使用


更新 在您的特定场景中,没有日期。在这种情况下,您可以创建2个
时间跨度

var yourTotalMilliSeconds = 
                   (new DateTime(hours, minutes, seconds)TimeOfDay - DateTime.Now.TimeOfDay)
        .TotalMillisenconds;

首先,将其转换为日期时间:

//note: you can also use ParseExact if you need a specific format.
var dt = DateTime.Parse(currentTask.time);
然后,从当前时间中减去它,结果是

您的结果位于
totalmillizes
属性中。见:

请注意,这有一个约3-6毫秒的侵入。如果你需要更多的精度,请使用


更新 在您的特定场景中,没有日期。在这种情况下,您可以创建2个
时间跨度

var yourTotalMilliSeconds = 
                   (new DateTime(hours, minutes, seconds)TimeOfDay - DateTime.Now.TimeOfDay)
        .TotalMillisenconds;

此代码假定您使用的是UTC日期时间

如果你在当地时间工作,那么你很可能会遇到一些问题,比如转换到夏令时和从夏令时转换到夏令时

// The time of day in hh:mm
var rawTime = "13:00";

// Parse the time
var time = TimeSpan.Parse(rawTime);

// Todays date, with the parsed time
var then = DateTime.UtcNow.Date + time;

// Todays date with the current time
var now = DateTime.UtcNow;

// If the parsed time has already passed then move forward a day
if(then < now)
    then = then + TimeSpan.FromDays(1);

// How long until the parsed time
var duration = then - now;          
var durationInMilliseconds = duration.TotalMilliseconds;

Console.WriteLine(duration); // 22:35:41.4646691
Console.WriteLine(durationInMilliseconds + "ms"); // 81341464.6691ms
//一天中的时间,单位为hh:mm
var rawTime=“13:00”;
//解析时间
var time=TimeSpan.Parse(rawTime);
//今天的日期,以及解析的时间
然后var=DateTime.UtcNow.Date+time;
//今天日期与当前时间一致
var now=DateTime.UtcNow;
//如果解析的时间已经过去,则向前移动一天
如果(当时<现在)
then=then+TimeSpan.FromDays(1);
//距离解析时间还有多久
var持续时间=当时-现在;
var durationInMilliseconds=duration.TotalMilliseconds;
Console.WriteLine(持续时间);//22:35:41.4646691
Console.WriteLine(持续时间毫秒+“毫秒”);//81341464.6691ms

此代码假定您使用的是UTC日期时间

如果你在当地时间工作,那么你很可能会遇到一些问题,比如转换到夏令时和从夏令时转换到夏令时

// The time of day in hh:mm
var rawTime = "13:00";

// Parse the time
var time = TimeSpan.Parse(rawTime);

// Todays date, with the parsed time
var then = DateTime.UtcNow.Date + time;

// Todays date with the current time
var now = DateTime.UtcNow;

// If the parsed time has already passed then move forward a day
if(then < now)
    then = then + TimeSpan.FromDays(1);

// How long until the parsed time
var duration = then - now;          
var durationInMilliseconds = duration.TotalMilliseconds;

Console.WriteLine(duration); // 22:35:41.4646691
Console.WriteLine(durationInMilliseconds + "ms"); // 81341464.6691ms
//一天中的时间,单位为hh:mm
var rawTime=“13:00”;
//解析时间
var time=TimeSpan.Parse(rawTime);
//今天的日期,以及解析的时间
然后var=DateTime.UtcNow.Date+time;
//今天日期与当前时间一致
var now=DateTime.UtcNow;
//如果解析的时间已经过去,则向前移动一天
如果(当时<现在)
then=then+TimeSpan.FromDays(1);
//距离解析时间还有多久
var持续时间=当时-现在;
var durationInMilliseconds=duration.TotalMilliseconds;
Console.WriteLine(持续时间);//22:35:41.4646691
Console.WriteLine(持续时间毫秒+“毫秒”);//81341464.6691ms
var result=(Time1-Time2).total毫秒在您的例子中
var result=(DateTime.Now-DateTime.ParseExact(currentTask.time,“H:m”,CultureInfo.InvariantCulture)).total毫秒
var result=(Time1-Time2).total毫秒在您的例子中
var result=(DateTime.Now-DateTime.ParseExact(currentTask.time,“H:m”,CultureInfo.InvariantCulture)).total毫秒