C# 通过从当前日期中减去秒来获取 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8858683/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:50:16  来源:igfitidea点击:

Get a DateTime by subtracting seconds from the current date

c#datetime

提问by Eric

I would like to subtract seconds from a date, for example:

我想从日期中减去秒数,例如:

Lets say I have 1300 seconds in a unsigned integer, I would like to take the current date and time, subtract 1,300 seconds from it, and end up with:

假设我在一个无符号整数中有 1300 秒,我想获取当前日期和时间,从中减去 1,300 秒,最后得到:

01/13/2012 2:15 PM(format doesn't really matter).

01/13/2012 2:15 PM(格式并不重要)。

I did try:

我确实尝试过:

DateTime dt = new DateTime(); 
dt.Add(new TimeSpan(0, 0, ui.OnlineTime)); 
Online.Text = dt.ToLongDateString();

采纳答案by dasblinkenlight

AddSeconds(double value)method of DateTimetakes positive and negative number of seconds:

AddSeconds(double value)的方法DateTime需要几秒钟正和负号:

[valueparameter represents] a number of whole and fractional seconds. The value parameter can be negative or positive.

[value参数代表] 整数和小数秒数。value 参数可以是负数或正数。

Hence, to subtract 1300 seconds you need to add -1300 seconds:

因此,要减去 1300 秒,您需要添加 -1300 秒:

DateTime.Now.AddSeconds(-1300);

回答by SLaks

DateTime.Now.AddSeconds(-1300)

That was easy

那很简单

回答by tobias86

Try:

尝试:

DateTime.Now.AddSeconds(-1300);