Child pages
  • Working with Dates (and timezones)

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleExample of local or UTC time with getHours()
    var date1 = new Date(2019,10,1,12);
	var date2 = new Date(Date.UTC(2019,10,1,12));
	application.output(date1)
	application.output(date2)
	application.output(date1.getHours())
	application.output(date2.getHours())

...

That will use the timezone of the client that we got from the browser. And for example if the server would be in +1 and the NGClient would be in +2 then the above code would print "13". Because "12:00" in the servers timezone is for the clients timezone 1 hour later. (Amsterdam compared to Athens)

Use as LocalDateTime

For displaying stuff in the client we he have for NGClient an option to Use as LocalDateTime in the date format dialog. By default if that is not checked we send the date over in a ISO-8601 dateformat like: 2019-11-01T12:00:00+01:00 if the server is in +1 . So we send over the date in the servers format but we also give the timezone where we formatted this date in. This information is received by a client and if that is in +2 the time that the client will display that same date will be:  2019-11-01 13:00, because it knows that that give date was for +1 and it is in +2 so it must add 1 hour to the displayed time (still the time in milliseconds does not change! this is purely a formatting thing)

...

The browser if it changes the date and we send over the date string again it will always just send over the timezone info with it, sending it always in the format: 2019-11-01T13:00:00+02:00, but the server knows that for that property it should use LocalDateTime and will just ignore or not that timezone information based on that boolean.

application.getTimeStamp()

If you are using "Use as LocalDateTime" then it could be that you want to make the date object that must be really in "today" or "now" on the client. So the same thing as doing a new Date() in the browser and then sending that to the server that ignored the timezone.

To do the same thing that you really are in the "now" on the client you can use application.getTimeStamp() this will create a date that will be in the same day as the browser. as an example new Date() on the server that is in +1 would generate 2019-28-11 23:00 then application.getTimeStamp() would generate if the client was in +2 a date string of 2019-28-12 00:00. Which is really the time that that client is in at that time.

Searching

If you want to search between 2 dates, like searching for everything of yesterday, but then you mean everything of what the users means of yesterday.  You need to make dates that for that user is yesterday.  Which could be dates like: "2019-28-10 00:00" and "2019-28-10 23:59", but those times are client times so in the current example in timezone +2. But the server is in +1 , and the server will do the searching if it does that with the 2 dates above as is then it would be searching for stuff that is in the "today" of the server. So you need to convert or create them first to the times of the user:


Code Block
languagejs
titleCreating dates in the client timezone
	var startDate = utils.parseDate("2019-11-28 00:00","yyyy-MM-dd HH:mm", null)
	var endDate = utils.parseDate("2019-11-28 23:59","yyyy-MM-dd HH:mm", null)


This parseDate with 3 arguments is new in Servoy 2019.12, the 3rd argument can also be a TimeZone ID like "GMT+3" but if it is null it will use the clients timezone, the standard 2 arguments will use the servers timezone.

The output of that if the server is in +1 and the client is in +2:

Wed Nov 27 23:00:00 CET 2019
Thu Nov 28 22:59:00 CET 2019

(dates printed/formatted in +1/CET)

If these 2 timestamps would be send to the client (Use as LocalDateTime as false), then the browser will add the time difference (1 hour) back on to it and it will then represent the exactly that day.