How to compare datetime in jasmine test?

#1

In my jasmine test, I have

obj.createdDate.toDate()
> Mon Oct 15 2007 17:00:00 GMT-0700 (Pacific Daylight Time)

and the following expect statement passes

expect(obj.createdDate).toEqual(DateTime('2000-01-01T00:00:00.000'));

How should I compare datetime?

0 Likes

#2

expect(obj.createdDate.getMillis()).toEqual(DateTime('2000-01-01T00:00:00.000').getMillis());

0 Likes

#3

What is the reasoning for requiring the datetime to be deconstructed into millis to compare it? Intuitively, I would think I could just have a comparison like ‘start != end’, but that doesn’t seem to work. Is there some meta data or something that’s being considered that is preventing a comparison by just the underlying time?

1 Like

#4

I believe the issue is that toEqual is a jasmine function, and it is checking for equality between javascript primitive types. The DateTime type is just of type object with respect to jasmine since it doesn’t have any concept of the C3 type system. When you use toEqual jasmine is checking for object equality rather than value.

0 Likes

#5

You can use DateTime.dateDiff as in

1 Like

#6

@bachr didn’t know about that one! you can also do DateTime.compareTo():

expect(obj.createdDate.compareTo(DateTime('2000-01-01T00:00:00.000'))).toEqual(0);
0 Likes

#7

I prefer DateTime.isEqual() for checking equality:

expect(obj.createdDate.isEqual(DateTime('2000-01-01T00:00:00.000'))).toBe(true);
0 Likes