cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

date-from-array.js (1076B)


      1export function createDate(y, m, d, h, M, s, ms) {
      2    // can't just apply() to create a date:
      3    // https://stackoverflow.com/q/181348
      4    var date;
      5    // the date constructor remaps years 0-99 to 1900-1999
      6    if (y < 100 && y >= 0) {
      7        // preserve leap years using a full 400 year cycle, then reset
      8        date = new Date(y + 400, m, d, h, M, s, ms);
      9        if (isFinite(date.getFullYear())) {
     10            date.setFullYear(y);
     11        }
     12    } else {
     13        date = new Date(y, m, d, h, M, s, ms);
     14    }
     15
     16    return date;
     17}
     18
     19export function createUTCDate(y) {
     20    var date, args;
     21    // the Date.UTC function remaps years 0-99 to 1900-1999
     22    if (y < 100 && y >= 0) {
     23        args = Array.prototype.slice.call(arguments);
     24        // preserve leap years using a full 400 year cycle, then reset
     25        args[0] = y + 400;
     26        date = new Date(Date.UTC.apply(null, args));
     27        if (isFinite(date.getUTCFullYear())) {
     28            date.setUTCFullYear(y);
     29        }
     30    } else {
     31        date = new Date(Date.UTC.apply(null, arguments));
     32    }
     33
     34    return date;
     35}