Eric
Eric

Reputation: 1268

Get a DateTime by subtracting seconds from the current date

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();

Upvotes: 24

Views: 39672

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

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);

Upvotes: 44

tobias86
tobias86

Reputation: 5029

Try:

DateTime.Now.AddSeconds(-1300);

Upvotes: 5

SLaks
SLaks

Reputation: 887285

DateTime.Now.AddSeconds(-1300)

That was easy

Upvotes: 4

Related Questions