Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a DateTime by subtracting seconds from the current date

Tags:

c#

datetime

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:

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

I did try:

DateTime dt = new DateTime(); 
dt.Add(new TimeSpan(0, 0, ui.OnlineTime)); 
Online.Text = dt.ToLongDateString();
like image 258
Eric Avatar asked Jan 13 '12 23:01

Eric


2 Answers

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

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

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

DateTime.Now.AddSeconds(-1300);
like image 79
Sergey Kalinichenko Avatar answered Nov 11 '22 13:11

Sergey Kalinichenko


Try:

DateTime.Now.AddSeconds(-1300);
like image 21
tobias86 Avatar answered Nov 11 '22 13:11

tobias86