$jsapi.dateForZone
This method returns the current date and time in the specified format.
Syntax
The method accepts two string arguments:
- The time zone ID as per the Java specification.
- The date and time format pattern as per the
SimpleDateFormat
Java object reference.
The method returns a string.
$jsapi.dateForZone("Europe/Berlin", "dd.MM"); // => "23.06"
Usage details
-
Another way to retrieve the formatted date and time is to use the
$jsapi.timeForZone
method in combination with theformat
method from the Moment.js library.cautionThemoment.format
and$jsapi.dateForZone
methods use different date and time pattern formats. The following is a notable difference:Format Java Moment.js D
Day in year, from 1 to 365 or 366 Day in month, from 1 to 31 d
Day in month, from 1 to 31 Day of week, from 0 (Sunday) to 6 (Saturday) -
If your project uses
$jsapi.dateForZone
and has automated tests, you need to override the time the tests will treat as their current time. Use the<dateTime>
tag to do this.
How to use
# Here, we import the cities dictionary from the zb-common system project.
require: city/cityEn.sc
module = sys.zb-common
theme: /
state: CurrentLocalTime
q!: * (what time is it/what ('s/is) the time) * [in] $City *
script:
$temp.time = $jsapi.dateForZone($parseTree._City.timezone, "h.mm a");
a: Right now, it’s {{$temp.time}} in {{$parseTree._City.name}}.