1

I have one field in database in this format: 2013-06-18 17:00:00.000 and second field Duration in this format: 3000 (this represents seconds, so it is 50 minutes)

I need to subtract those two fields and to set in another field result which will be: 2013-06-18 16:10:00.000

One addition is that they both can be retrieved from database in string format only. So they are both strings.

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Veljko
  • 1,708
  • 12
  • 40
  • 80

3 Answers3

4

First you need to Parse the datetime. Then subtract using AddSeconds:

var date = DateTime.Parse("2013-06-18 17:00:00.000");
var newDate = date.AddSeconds(int.Parse("-3000"));

You can use newDate.ToString() to get the date as a string.

You can find the documentation for DateTime here.

Update: Changed seconds to a string value. Which uses Parse to convert to an integer.

Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • are we missing newDate.ToString() since we need it in string format – Veljko Jan 09 '16 at 18:50
  • One question: var type represents DateTime variable? like Date in Java or not? Thanks – Veljko Jan 09 '16 at 18:57
  • 2
    Well sort of. In c# you have the keyword "var". It can represent any type actually. You can change var to "DateTime" in my example if you think that is more clear. However most find it more readable when they get used to it (even though that can take some time). You can read more about var here: http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp – Peter Rasmussen Jan 09 '16 at 19:02
  • It might also be good to note that DateTime.Parse will throw an exception if the input string isn't in an acceptable format. This is nice to be aware of if you don't trust your input source. – AJ X. Jan 09 '16 at 19:40
2

You can subtract to the datetime object. (if is a DateTime Type) if not, you should parse.

To handle errors, I would recommend to use DateTime.tryParse(value, out dateTime);

DateTime parsedDateFromBD;

if(DateTime.tryParse("2013-06-18 17:00:00.000", out parsedDateFromBD)
{
    // do Stuff
}
else
{
   // do something else
}

if you get it as a datetime from the db you can simply:

var calcDate1 = dateFromBD.addSeconds(3000); //to Add
var calcDate2 = dateFromBD.addSeconds(-3000); //to subtract

Cheers

Ricardo

rmjoia
  • 962
  • 12
  • 21
  • 1
    There's absolutely no need to call ` = new Datetime();` – Camilo Terevinto Jan 09 '16 at 18:53
  • @cFrozenDeath using the `var` KeyWord you have to. It cannot infer the type. But you could also use: `DateTime parsedDateFromBD;` maybe even better for performance. Sure. – rmjoia Jan 09 '16 at 18:56
  • Which would be definitely the better way as in your original answer since the instance created through the `new DateTime()` would be overriden anyway by the TryParse operation. – Manuel Zelenka Jan 09 '16 at 19:00
  • Agree, I will change that. Thank you both cFrozenDeath and @ManuelZelenka – rmjoia Jan 09 '16 at 19:20
1

In addition to the other answers here is how to parse the newDate to string that mach the required output

string date = "2013-06-18 17:00:00.000";
string duration = "-3000";

int durationSeconds = int.Parse(duration);

var newDate = DateTime.Parse(date).AddSeconds(durationSeconds).ToString("yyyy-MM-dd HH:mm:ss.fff");

The output is

//2013-06-18 16:10:00.000

Here you can find more about DateTime.ToString()

Kos
  • 567
  • 4
  • 15