timezonedate
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,3 +5,3 @@ { | ||
"description": "A JavaScript Date alternative that is specific to a given timezone other than the current machine's timezone.", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Bob Lauer <rlauer@gmail.com>", | ||
@@ -8,0 +8,0 @@ "main": "./src/timezonedate.js", |
@@ -5,1 +5,72 @@ TimeZoneDate | ||
A JavaScript Date alternative that is specific to a given timezone other than the current machine's timezone. | ||
## Installation ## | ||
`npm install timezonedate` | ||
## Environment Support ## | ||
TimeZoneDate has been tested in Node, IE9+, Chrome, Firefox, and Opera. | ||
## Usage ## | ||
```javascript | ||
// CommonJS | ||
var TimeZoneDate = require('timezonedate'); | ||
``` | ||
```javascript | ||
// AMD | ||
require(['timezonedate'], function(TimeZoneDate) { ... }); | ||
``` | ||
```javascript | ||
// Script Tag | ||
var TimeZoneDate = window.TimeZoneDate; | ||
``` | ||
## API ## | ||
```javascript | ||
function TimeZoneDate(offset[, initialDate]) | ||
``` | ||
#### __offset__ | ||
__type__: __`Number`__ | ||
There are two ways to specify a timezone offset. | ||
The easiest is by passing in the hours relative to UTC. So if you want to specify the timezone one hour behind UTC, you would pass in -1. For one hour ahead of UTC, you would pass in 1. | ||
The other way to specify the timezone is to pass in what the result of `new Date().getTimezoneOffset()` would be if it were run in the target timezone. For example, if your system clock is set to UTC-1, `new Date().getTimezoneOffset()` will be 60. If you are in UTC+1, the result will be -60. Check out [the MDN getTimezoneOffset documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset) for more info. | ||
#### __initialDate__ | ||
__type__: __`Date` or `String`__ | ||
If a Date object is passed in, the TimeZoneDate will be set to the same moment, rather than the same time. For example: | ||
```javascript | ||
// Assume the current machine is set to UTC-1 | ||
var dateInUTCMinus1 = new Date('1/1/2014 12:00:00'); | ||
var dateInUTCPlus1 = new TimeZoneDate(1, dateInUTCMinus1); | ||
dateInUTCMinus1.valueOf() === dateInUTCPlus1.valueOf(); | ||
// true | ||
dateInUTCMinus1.toString(); | ||
// Wed Jan 01 2014 12:00:00 GMT-0100 | ||
dateInUTCPlus1.toString(); | ||
// Wed Jan 01 2014 14:00:00 GMT+0100 | ||
``` | ||
If a string is passed in, the TimeZoneDate will be set to that date/time. For example: | ||
```javascript | ||
// Assume the current machine is set to UTC-1 | ||
var dateInUTCMinus1 = new Date('1/1/2014 12:00:00'); | ||
var dateInUTCPlus1 = new TimeZoneDate(1, '1/1/2014 12:00:00'); | ||
dateInUTCMinus1.valueOf() === dateInUTCPlus1.valueOf(); | ||
// false | ||
dateInUTCMinus1.toString(); | ||
// Wed Jan 01 2013 12:00:00 GMT-0100 | ||
dateInUTCPlus1.toString(); | ||
// Wed Jan 01 2013 12:00:00 GMT+0100 | ||
``` | ||
If the `initialDate` parameter is omitted, it is the equivalent of passing in `new Date()`. | ||
__The rest of the API is exactly the same as the native Date object.__ |
@@ -10,6 +10,4 @@ (function(name, definition) { | ||
if (!date) { | ||
this._date = this._getDateWithTargetOffsetAdded(new Date()); | ||
date = this._date.toString(); | ||
} | ||
this._date = date ? new Date(date) : this._getDateWithTargetOffsetAdded(new Date()); | ||
date = this._date.toString(); | ||
@@ -100,2 +98,10 @@ this._date = new Date(date); | ||
TimeZoneDate.prototype.getTimezone = function() { | ||
return ''; | ||
}; | ||
TimeZoneDate.prototype.getTimezoneOffset = function() { | ||
return this._offset; | ||
}; | ||
TimeZoneDate.prototype.toJSON = function() { | ||
@@ -105,3 +111,3 @@ return this._date.toJSON(); | ||
TimeZoneDate.prototype.valueOf = function() { | ||
TimeZoneDate.prototype.valueOf = TimeZoneDate.prototype.getTime = function() { | ||
return this._date.valueOf(); | ||
@@ -108,0 +114,0 @@ }; |
@@ -5,2 +5,4 @@ var assert = chai.assert; | ||
var utcDate = new TimeZoneDate(0, '1/1/2013 11:00 PM'); | ||
var utcMinus3 = new TimeZoneDate(-3); | ||
var utcPlus3 = new TimeZoneDate(-180); | ||
@@ -19,2 +21,8 @@ it('should create a new TimeZoneDate object', function() { | ||
}); | ||
it('should give back the correct offset', function() { | ||
assert.equal(utcDate.getTimezoneOffset(), 0); | ||
assert.equal(utcMinus3.getTimezoneOffset(), 180); | ||
assert.equal(utcPlus3.getTimezoneOffset(), -180); | ||
}); | ||
}); | ||
@@ -21,0 +29,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
246292
8872
76