From 2c501fc0bfffb87376fbc9e2e6bb291ba6a19074 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 28 Sep 2016 16:27:32 -0400 Subject: [PATCH] JBAS-1377 Date handling utility JS functions Signed-off-by: Bill Erickson Conflicts: Open-ILS/web/js/dojo/openils/Util.js --- Open-ILS/web/js/dojo/openils/Util.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Open-ILS/web/js/dojo/openils/Util.js b/Open-ILS/web/js/dojo/openils/Util.js index e3588ae61f..3482fb673e 100644 --- a/Open-ILS/web/js/dojo/openils/Util.js +++ b/Open-ILS/web/js/dojo/openils/Util.js @@ -508,5 +508,34 @@ if(!dojo._hasResource["openils.Util"]) { } }; + openils.Util.getNodeByName = function(name, contextNode) { + return dojo.query('[name=' + name + ']', contextNode)[0]; + }; + + // Returns the provided date as YYYY-MM-DD for the local time zone. + // If no date is provided, the current date is used. + openils.Util.getYMD= function(d) { + if (!d) d = new Date(); + + // toISOString returns a UTC date. Mangle our 'now' date to + // force its UTC verion to match that of the local version + // once the timezone is stripped away. + // E.g. if the local time zone is -0500, subract 5 hours + // from the date before translating to an ISO string. + // Note: tz offset is positive for locales behind UTC. + d.setTime(d.getTime() - (d.getTimezoneOffset() * 60 * 1000)); + + return d.toISOString().replace(/T.*/, ''); + } + + // Creates a date in the local time zone from the provided YYYY-MM-DD. + // Parsing a bare YYYY-MM-DD value creates a UTC date, causing the + // resulting date to be off by a day in the local time zone. + openils.Util.fromYMD = function(ymd) { + if (!ymd) return null; + var parts = ymd.split('-'); + return new Date(parts[0], Number(parts[1]) - 1, Number(parts[2])); + } + } -- 2.11.0