JBAS-1377 Date handling utility JS functions
authorBill Erickson <berickxx@gmail.com>
Wed, 28 Sep 2016 20:27:32 +0000 (16:27 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Conflicts:
Open-ILS/web/js/dojo/openils/Util.js

Open-ILS/web/js/dojo/openils/Util.js

index e3588ae..3482fb6 100644 (file)
@@ -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]));
+    }
+
 }