new Date() returns wrong date

Questions, tips and tricks and techniques for scripting in Servoy

new Date() returns wrong date

Postby maria » Mon Nov 29, 2010 2:03 pm

Hi All,

new Date() returns a date one hour later than current time.
I'm running my solution in Developer, both client and server on one machine.
The server page shows correct time on the server.
What's wrong?

Cheers,
Maria
maria
 
Posts: 424
Joined: Thu Apr 16, 2009 1:18 am
Location: Sydney

Re: new Date() returns wrong date

Postby Andrei Costescu » Mon Nov 29, 2010 4:39 pm

Are you running the solution in web client or smart client?
What is the value of "servoy.use.client.timezone" property in servoy.properties?
How did you see the date value created with new Date()? In a field, via application.output(...) or some other way?
What version of Servoy are you running?
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: new Date() returns wrong date

Postby maria » Tue Nov 30, 2010 12:24 am

Andrei Costescu wrote:Are you running the solution in web client or smart client?
What is the value of "servoy.use.client.timezone" property in servoy.properties?
How did you see the date value created with new Date()? In a field, via application.output(...) or some other way?
What version of Servoy are you running?


Ok...
1. I'm running the solution in web client.
2. 'servoy.use.client.timezone' is true. Not sure how this is going to work if webclient is 'sort of' running serverside.
3. The new Date() value comes up the same both via application.output() and via ctrl+shift+i during debugging.
4. It's Servoy version 5.1.4 -build 964. We're reluctant to upgrade it.

Cheers,
Maria
maria
 
Posts: 424
Joined: Thu Apr 16, 2009 1:18 am
Location: Sydney

Re: new Date() returns wrong date

Postby Andrei Costescu » Tue Nov 30, 2010 10:57 am

Maybe the developer (that means both server and smart client under developer) is running in a different timezone.
Please check you smart client preferences to see if the default timezone that is set explains what is happening (that timezone will also be the timezone of the server under developer).
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: new Date() returns wrong date

Postby imre_tokai » Wed Nov 14, 2012 12:26 pm

Hello,
Have this issue when running WebClient 5.2.15
new Date() gives the time on the server (what makes sense in basic theory as WebClient code is running server-side) but need there client's datetime as I configure certain time on the client and than when time matching on server action is triggered. Can do only if client & server are in same timezone (client's -user friendly).
servoy.use.client.timezone is true

WebClientUtils is suggested by Patrick in other thread, is that the optimal way to get client time zone's new Date()?
https://www.servoyforge.net/projects/webclientutils

Regards
Imre Tokai, Software Engineer
iTech Professionals, Inc.
User avatar
imre_tokai
 
Posts: 33
Joined: Mon Mar 26, 2012 12:18 pm

Re: new Date() returns wrong date

Postby Andrei Costescu » Wed Nov 14, 2012 12:41 pm

So do you want the time as reported by the client's OS or the client's time computed based on client timezone and server time?
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: new Date() returns wrong date

Postby imre_tokai » Wed Nov 14, 2012 3:06 pm

Thanks Andrei, good point

Option 2 (server time in client's timezone) has been built
-collecting offset on form's onShow() event
Code: Select all
var js = "var d = new Date();var offset = d.getTimezoneOffset();";
plugins.WebClientUtils.executeClientSideJS( js, globals.callbackOffset, ['offset'] );

-dividing collected offset with 60
-subtracting the hours from server hours
Code: Select all
date.setHours(date.getHours() - forms.frm_time_zone_sync_server_client.offset )


Let me know if this is completely generic and/or if anyone have better (optimal) work-flow, please

Regards
Imre Tokai, Software Engineer
iTech Professionals, Inc.
User avatar
imre_tokai
 
Posts: 33
Joined: Mon Mar 26, 2012 12:18 pm

Re: new Date() returns wrong date

Postby Andrei Costescu » Thu Nov 15, 2012 10:09 am

You should get the same result from application.getTimestamp() since 6.0.8 (SVY-2726).
Before - in 5.x - it either simply returned a new Date() which isn't helpful for WC or in later versions it would not always be correct depending on the type of client and servoy.use.client.timezone.
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 10 guests