Page 1 of 1

pass a Date by value

PostPosted: Tue Jun 28, 2011 7:41 pm
by mo_ca_mo
Hello,
There is a way to pass a Date by value?
I use the follows method with a Date: "fechaEnvio" to calc the better option
Code: Select all
function obtenerDif(fechaEnvio, calendex){
   var dias = 0
   calendex--
   
   while(fechaEnvio.getDay() != calendex){
      dias++
      fechaEnvio.setDate(fechaEnvio.getDate() + 1)
   }
   return dias
}

but when the method finish the Date is changed because it was passed by reference not by value
i solved the problem creating a new date and seting the tieme to it
But i would like to know how to pass a data by value
thanks

Re: pass a Date by value

PostPosted: Wed Jun 29, 2011 10:06 am
by ROCLASI
Hi,

mo_ca_mo wrote:But i would like to know how to pass a data by value


Objects are always passed by reference. Strings and Numbers are passed by value. So if you really want your date to be passed by value you need to pass it as a string or number like so:
Code: Select all
function myNumberDate(_nDate) {
    application.output("date from Number: " + new Date(_nDate));
}

function myStringDate(_sDate) {
    application.output("date from String: " + new Date(_sDate));
}

var myDate = new Date();
application.output("original date: " + myDate);

myStringDate(myDate.toUTCString()); //pass it as a String
myNumberDate(myDate.valueOf()); // pass it as a Number (in milliseconds)


Hope this helps.