Datetime.now作为TimeSpan值? [英] Datetime.now as TimeSpan value?

查看:383
本文介绍了Datetime.now作为TimeSpan值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的当前日期时间减去 myDate1 以秒为单位。

I need the current Datetime minus myDate1 in seconds.

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;

TimeSpan myDateResult = new TimeSpan();

myDateResult = myDate2 - myDate1;



。结果
。结果
我尝试不同的方法来计算但没有效果。

.
.
I tried different ways to calculate but to no effect.

TimeSpan mySpan = new TimeSpan(myDate2.Day, myDate2.Hour, myDate2.Minute, myDate2.Second);



。结果
它的计算方式并不重要,输出应该只是在秒值的差异这些。

.
The way it's calculated doesn't matter, the output should just be the difference these to values in seconds.

推荐答案

您的代码是正确的。你有时间差为时间跨度价值,所以你只需要使用 TotalSeconds 属性来获取它作为秒

Your code is correct. You have the time difference as a TimeSpan value, so you only need to use the TotalSeconds property to get it as seconds:

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;

TimeSpan myDateResult;

myDateResult = myDate2 - myDate1;

double seconds = myDateResult.TotalSeconds;

这篇关于Datetime.now作为TimeSpan值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆