View source Improve this doc

angular.mock.TzDate

API in module ng

Description

NOTE: this is not an injectable instance, just a globally available mock class of Date.

Mock of the Date type which has its timezone specified via constructor arg.

The main purpose is to create Date-like instances with timezone fixed to the specified timezone offset, so that we can test code that depends on local timezone settings without dependency on the time zone settings of the machine where the code is running.

Usage

  1. TzDate(offset, timestamp);

Parameters

ParamTypeDetails
offsetnumber Offset of the desired timezone in hours (fractions will be honored)
timestampnumberstring Timestamp representing the desired time in UTC

Example

!!!! WARNING !!!!! This is not a complete Date object so only methods that were implemented can be called safely. To make matters worse, TzDate instances inherit stuff from Date via a prototype.

We do our best to intercept calls to "unimplemented" methods, but since the list of methods is incomplete we might be missing some non-standard methods. This can result in errors like: "Date.prototype.foo called on incompatible Object".

  1. var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00Z');
  2. newYearInBratislava.getTimezoneOffset() => -60;
  3. newYearInBratislava.getFullYear() => 2010;
  4. newYearInBratislava.getMonth() => 0;
  5. newYearInBratislava.getDate() => 1;
  6. newYearInBratislava.getHours() => 0;
  7. newYearInBratislava.getMinutes() => 0;
  8. newYearInBratislava.getSeconds() => 0;