node-time
This module offers simple bindings for the C time.h APIs.
It also offers an extended regular Date
object with getTimezone()
and setTimezone()
functions, which aren't normally part of JavaScript.
Example
var time = require('time');
var now = new time.Date();
now.setTimezone("America/Los_Angeles");
now.setTimezone("America/New_York");
API
Date() -> Date
A special Date
constructor that returns a "super" Date instance, that has
magic timezone capabilities!
var date = new time.Date();
date.setTimezone(timezone) -> Undefined
Sets the timezone for the Date
instance. Calls to getHours()
, getDays()
,
getMinutes()
, etc. will be relative to the timezone specified. This will throw
an Error if information for the desired timezone could not be found.
date.setTimezone("America/Argentina/San_Juan");
date.getTimezone() -> String
Returns a String containing the currently configured timezone for the date instance.
date.getTimezone();
date.getTimezoneAbbr() -> String
Returns the abbreviated timezone name, also taking daylight savings into consideration.
Useful for the presentation layer of a Date instance. This is a NON-STANDARD extension
to the Date object, and must be called after setTimezone()
.
date.getTimezoneAbbr();
extend(date) -> Date
Transforms a "regular" Date instance into one of node-time
's "extended" Date instances.
var d = new Date();
time.extend(d);
d.setTimezone("UTC");
time() -> Number
Binding for time()
. Returns the number of seconds since Jan 1, 1900 UTC.
These two are equivalent:
time.time();
Math.floor(Date.now() / 1000);
tzset(timezone) -> Object
Binding for tzset()
. Sets up the timezone information that localtime()
will
use based on the specified timezone variable, or the current process.env.TZ
value if none is specified. Returns an Object containing information about the
newly set timezone, or throws an Error if no timezone information could be loaded
for the specified timezone.
time.tzset('US/Pacific');
Binding for localtime()
. Accepts a Number with the number of seconds since the
Epoch (i.e. the result of time()
), and returns a "broken-down" Object
representation of the timestamp, according the the currently configured timezone
(see tzset()
).
time.localtime(Date.now()/1000);