hersh
hersh

Reputation: 1183

convert datetime js to c#

How would I convert this from js to C#. I've looked but cant find anything useful.

var endDate = new DateTime();
endDate.setDate(startDateTime.getDate() + days);
endDate.setHours(endDateTime.getHours(), endDateTime.getMinutes(), endDateTime.getSeconds());

Upvotes: 1

Views: 277

Answers (1)

Ani
Ani

Reputation: 113402

Assuming that you don't mind sub-second precision on endDateTime, you could use:

var endDate = startDateTime.Date.AddDays(days).Add(endDateTime.TimeOfDay);                                               

If you do mind, this should do the trick:

var offSet = new TimeSpan(days, endDateTime.Hour, endDateTime.Minute, endDateTime.Second);
var endDate = startDateTime.Date.Add(offSet);

Upvotes: 2

Related Questions