pass a Date by value

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

pass a Date by value

Postby mo_ca_mo » Tue Jun 28, 2011 7:41 pm

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
mo_ca_mo
 
Posts: 72
Joined: Wed Feb 03, 2010 8:13 pm

Re: pass a Date by value

Postby ROCLASI » Wed Jun 29, 2011 10:06 am

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.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5292
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 2 guests

cron