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 is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
Amit
  • 6,203
  • 21
  • 49
  • 87

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 is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
  • 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 is supporting Monica 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 is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
Travis J
  • 77,009
  • 39
  • 185
  • 250
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
  • 23,986
  • 21
  • 87
  • 119
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
  • 104,388
  • 28
  • 131
  • 164