0

I have this below code in a javascript file. When i run it i get error message :

"Can't find variable: addZero".

function addZero(n) {
    return ( n < 0 || n > 9 ? "" : "0" ) + n;
}

Date.prototype.toISODate =
        new Function("with (this)\n    return " +
           "getFullYear()+'-'+ addZero(getMonth()+1)+ '-'" +
           "+ addZero(getDate()) + 'T' + addZero(getHours())+':' " +
           "+ addZero(getMinutes()) +':'+ addZero(getSeconds()) +'.000Z'");
gdoron
  • 147,333
  • 58
  • 291
  • 367
Amit
  • 6,839
  • 21
  • 56
  • 90

4 Answers4

1
function addZero(n) {
    return ( n < 0 || n > 9 ? "" : "0" ) + n;
}

Date.prototype.toISODate = function() {
    // do what you want here
    // with real code! not strings...
}​
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • are you suggesting some thing like Date.prototype.toISODate= function() { return getFullYear()+'-'+addZero(getMonth()+1)+ '-' +addZero(getDate())+'T'+addZero(getHours())+':' +addZero(getMinutes())+':'+addZero(getSeconds())+'.000Z'; } – Amit May 01 '12 at 10:28
  • @Amit. yes, something like that. But I still don't know what you're tying to do and if it's the best way of doing it. – gdoron May 01 '12 at 10:29
0

Looks like your quotes are off. Try

return "with (this)\n    return " +
getFullYear() + '-' + addZero(getMonth()+1) + '-' +
addZero(getDate()) + 'T' + addZero(getHours())+':' +
addZero(getMinutes()) +':'+ addZero(getSeconds()) +'.000Z';
gdoron
  • 147,333
  • 58
  • 291
  • 367
Travis J
  • 81,153
  • 41
  • 202
  • 273
0

Theres a good function on the Mozilla Javascript reference page for Date that produces ISO Date strings

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Date

    /* use a function for the exact format desired... */
function ISODateString(d){
  function pad(n){return n<10 ? '0'+n : n}
  return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'
}

var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z
reach4thelasers
  • 26,181
  • 22
  • 92
  • 123
0

Try rewriting your Date extension like this, to keep things clear and to avoid using the with keyword:

Date.prototype.toISODate =
  function(){

    function padLeft(nr,base,padStr){
        base = base || 10;
        padStr = padStr || '0';
        var  len = (String(base).length - String(nr).length)+1;
        return len > 0? new Array(len).join(padStr)+nr : nr;
    }    

    return [this.getFullYear(),
            '-',
            padLeft(this.getMonth()+1),
            '-',
            padLeft(this.getDate()),
            'T', 
            padLeft(this.getHours()),
            ':',
            padLeft(this.getMinutes()),
            ':',
            padLeft(this.getSeconds()),
            '.',
            padLeft(this.getMilliseconds(),100),
            'Z'].join('');
  };

The padLeftZero function now exists within the scope of the Date.toISODate method. The use of an array literal to build the return string is for clarity. It isn't necessary and even can be called bad practice to use new Function ... to assign a function to Date.prototype.toISODate. BTW, milliseconds are added to the result (padded with zero's).

KooiInc
  • 119,216
  • 31
  • 141
  • 177