global variable date default anomaly...

I’m trying to set a DateTime global variable (globals.dateNotEarlierThan) to use as a check for entering wrong dates. I would like this default date to be 3 years prior to the current date. When I write a little javascript this seems to work fine in a method:

var dateToCheck = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate())

And that yields “Fri Jun 13 00:00:00 PDT 2008” as it should.

However when I use the same code in defining the default variable:

/**
 * @properties={typeid:35,uuid:"C5CB73D3-7F2D-41A9-A542-C3CF6533CE97",variableType:93}
 */
var g_dateNotEarlierThan = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate());

I get this as the default value for the global variable: “Wed Dec 31 16:00:00 PST 1969”

Any help for what I am doing wrong? This seems bizarre to me.

Hello John,

the following code yields the same (correct) value for both variables, on my computer here in Switzerland.
I have also tried it in the OnSolutionOpen event, cannot emulate the behaviour you get.
Possibly the behaviour is country-specific?

Regards

Oswald

/**

  • Perform the element default action.
  • @param {JSEvent} event the event that triggered the action
  • @properties={typeid:24,uuid:“3F1F6AC1-0277-4E3B-B9C7-0C366769286E”}
    */
    function onTestDate(event) {
    var dateToCheck = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate());
    application.output(dateToCheck);
    g_date(event)
    }

/**

  • Perform the element default action.
  • @param {JSEvent} event the event that triggered the action
  • @properties={typeid:24,uuid:“FDB31E68-E833-4726-8F35-D75E619EF362”}
    */
    function g_date(event) {
    globals.dateNotEarlierThan = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate());
    application.output(globals.dateNotEarlierThan);
    }

Hi John,

I see the same thing.
With the following code…

var g_dateNotEarlierThan = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate());

function onOpen() {
	var dateToCheck = new Date(new Date().getFullYear() - 3, new Date().getMonth(), new Date().getDate());
	application.output(globals.g_dateNotEarlierThan);
	application.output(dateToCheck);
}

I get the following results:

Thu Jan 01 01:00:00 CET 1970
Sat Jun 14 00:00:00 CEST 2008

John,

Servoy initialises the global by parsing the string, it however does not really run the javascript.
In this complex case it fails parsing and reverts to a zero-date.

When you initialize the global in your solution on-open method it will parse the date correctly.

Rob

Thanks everyone for chipping in. And Rob thanks for trying to set me straight about what a global variable default value really means. Unfortunately it still eludes me a little! :)

Initialize the global? I thought by simply opening the solution all globals would have their ‘default’ value ‘initialized’. I’m sorry I must have been really stupid and missed something along the way but what does that mean to ‘initialize’ a global variable? Do you mean I should just run that javascript to set the value on opening the solution, i.e. have just ‘null’ as the default date value and then set it in my opening method?

And when setting the global variable default values they don’t really run the javascript? I don’t quite get that nor what ‘parsing the string’ means in this case…what/why does it need to parse the string? And does that ‘failing to parse’ show up anywhere in a log? I know questions, questions but just trying to get my head around what the default values mean for global variables…

Hi John,

When you say ‘var mynum = 10;’ in globals.js, the global will get value 10 (initialized) when first accessed.

This works for simple values like 10 because the expression is parsed (converted to an object) and not really executed as an expression.

If you declare your global with null and give it a value on solution-onopen you run any javascript.

Rob