# calculating with dateinterval and nanoseconds

I am trying to calculate with nanoseconds in DateIntervals or dateTimes. In the following code, “di.Nanoseconds = 200000000” produces a NilObjectExeption. What is wrong here?

``````// test 1
var d, dr as DateTime
var di as DateInterval
d = DateTime.Now
di.Nanoseconds = 200000000
dr = d - di``````

Also, is it not possible to use two intervals for calculation? When I try the following code, the bug states: Type DateInterval does not define "Operator_Subtract with DateInterval.

``````// test 2
var di, dr as DateInterval
var d As New DateInterval(0,0,0,5,23,6,8)
di.Nanoseconds = 200000000
dr = d - di``````

Is the code wrong, or can’t you really calculate with two DateIntervals? Thanks.

[quote=477763:@Dodo Hunziker]I am trying to calculate with nanoseconds in DateIntervals or dateTimes. In the following code, “di.Nanoseconds = 200000000” produces a NilObjectExeption. What is wrong here?

``` // test 1 var d, dr as DateTime var di as DateInterval d = DateTime.Now di.Nanoseconds = 200000000 dr = d - di ```[/quote]

You’ve declared di as a DateInterval but haven’t actually created it with New.

// test 1
var d, dr as DateTime
var di as new DateInterval(0, 0, 0, 0, 0, 0, 200000000)
d = DateTime.Now
dr = d - di

DateInterval is for manipulating (adding/subtracting time) on a DateTime object. You can’t use one DateInterval on another, unfortunately.

Thanks Gavin for the quick reply! “new DateInterval” solved the problem. Only for a new one to occur, when I want to set a DateTime manually. The following code produces an InvalidArgumentException:

Another way to construct the date might be something like the following code. But running this one gets the message “Cannot assign a value to this property”

And “var t as new DateTime” is not accepted.

It’s not the NanoSecond parameter that is tripping you up, it’s this (from the docs):

``The oldest date DateTime can accept is 0001-01-01 00:00.``

Hey Gavin, you are great! Thanks a lot.