How to make a server side world time clock in xPages
-
03-07-2021 - |
Question
I want to display to users the time in different cities/timezones like this:
London 2012-02-01 11:30:00 GMT (0)
New York 2012-02-01 06:30:00 GMT (-6)
Stockholm 2012-02-01 12:30:00 GMT (+1)
I need it to be in ssjs and generic so that it does not matter which time zone the server is in.
I have been playing around with the following code lines but can not get it to work
var dt:NotesDateTime = session.createDateTime("Today");
dt.setNow()
//dt.convertToZone(-6,true)
//return dt.getGMTTime()
//return dt.getZoneTime()
GMT is ok, but users timezone is ok as well like GMT,UTC etc..
La solution
In this case I would stay away from the NotesDateTime object and use the Java Date object instead. You could do something like:
var d:java.util.Date = new java.util.Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
print(dateFormat.format(d));
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
print(dateFormat.format(d));
dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
print(dateFormat.format(d));
Remember that changing a timezone for a Java Date doesn't change the date itself, it just changes how it is displayed.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow