forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_util.js
42 lines (38 loc) · 1.74 KB
/
date_util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var moment = require('moment');
var tz = require('timezone/loaded');
var time = require('time');
module.exports = function(date, timezone) {
var strftime_format = '%F %T %z'; // used to convert a date into a normalized strftime format with timezone
var moment_format = 'YYYY-MM-DD HH:mm:ss zz'; // moment.js LDML format for parsing date strings
/**
* Convert a Javascript Date into node-time wrapper with the appropriate timezone.
* @param date {Date} Javascript Date object
* @param timezone {String} Olson timezone for this date (e.g. 'America/New_York')
* @return node-time object with the appropriate timezone
*/
var to_local = function(date, timezone) {
if (timezone == 'undefined') {
timezone = 'UTC';
}
var tz_date = new time.Date(date);
tz_date.setTimezone(timezone); // localize the date into the specified timezone
return local_datetime = tz(tz_date, strftime_format, timezone); // localized format w timezone offset
}
/**
* Convert a Javascript Date into a Moment.js moment obj with the appropriate timezone.
* Using the returned moment, you can call for example 'moment.calendar()' to get a
* human readable relative time such as 'last Friday at 3:52 PM'.
* @param date {Date} Javascript Date object
* @param timezone {String} Olson timezone for this date (e.g. 'America/New_York')
* @return moment with the appropriate timezone
*/
var to_moment = function(date, timezone) {
var local_datetime = to_local(date, timezone);
return moment(local_datetime, moment_format);
}
// return {
// to_local: to_local,
// to_moment: to_moment
// }
return to_moment(date, timezone);
}