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

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
   while(fechaEnvio.getDay() != calendex){
      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
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


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
Servoy Expert
Posts: 5297
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Return to Programming with Servoy

Who is online

Users browsing this forum: juan.cristobo and 4 guests