wrong date returned in Servoy 4.01

Hello,

the result for the variables of the function:

var vDate1 = new Date();
var vDate2 = new Date(vDate1.getFullYear(), vDate1.getMonth(), vDate1.getDay());

should be the same, or? But it isn’t!
I have created a function in Servoy 3.5 to set a specific date like:

var vPayDay = payment_condition; //date range for payment
var vDate1 = new Date(); //invoice date
var vDate2 = new Date(vDate1.getFullYear(), vDate1.getMonth(), vDate1.getDay() + vPayDay); //date of payment

and that is working correct! But in Servoy 4.01 vDate2 is always earlier than vDate1?!

Do anyone have the same issue?

Regards Thomas

Servoy Client
Version 4.0.1-build 628
Java version 1.5.0_16-133 (Mac OS X)

should be the same, or? But it isn’t!

I don’t think it should be the same.

This

var vDate1 = new Date();

creates a date from the current time, while this

var vDate2 = new Date(vDate1.getFullYear(), vDate1.getMonth(), vDate1.getDay());

creates a date where hours, minutes, seconds and milliseconds are 0. So vDate2 is always smaller than vDate1.

But from what I read between the lines, that is not your actual issue. What do you mean by “is always earlier than vDate1”? How much earlier?

Hi Patrick!

I tried a lot and changed the function variables to form variables, set hours, minutes, seconds and milliseconds of them to 0 and got 9 days earlier as result for 22.10.2008 00:00:00. By changing the date for example to a day in march 2008 the difference is only 3 to 5 days earlier than the vDate1?!
Could it may be a format (month - day) issue?

What exactly are you trying to do? Seems you want to add a number of days to a date? If yes, you should be able to do

var vDate1 = new Date(); //invoice date
var vMillisToAdd = (1000 * 60 * 60 * 24) * payment_condition
var vDate2 = new Date(vDate1.getTime() + vMillisToAdd ); //date of payment

getTime() returns the milliseconds starting from Jan, 1, 1970. new Date() accepts milliseconds in the constructor. So my method takes the ms from the current date and adds the number of milliseconds for your time offset. Then you construct a new date from that.

BTW: you can also use our DateUtils-Plugin. That eases this sort of math.

Hope this helps.

Yes, I would like to add a number of days to a given date. Your example looks good (professional) and I understand the logic. I will take it to my function and I am sure that works. But why does my posted function is working in Servoy 3.5.x and not in 4.0.1? However, don’t think too much about! Who cares while your solution example is working.

Thank you, Thomas

what i find weird is that getDay() works…

Because getDay() returns 0-6 (0 - sunday)

if you want to use the day in the month that you have to use: getDate()

this seems to add just 10 days:

var vPayDay = 10; //date range for payment
	var vDate1 = new Date(); //invoice date
	var vDate2 = new Date(vDate1.getFullYear(), vDate1.getMonth(), vDate1.getDate() + vPayDay); //date of payment