Skip to main content

$jsapi.dateForZone

This method returns the current date and time in the specified format.

Syntax

The method accepts two string arguments:

  1. The time zone ID as per the Java specification.
  2. 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 the format method from the Moment.js library.

    caution
    The moment.format and $jsapi.dateForZone methods use different date and time pattern formats. The following is a notable difference:
    FormatJavaMoment.js
    DDay in year, from 1 to 365 or 366Day in month, from 1 to 31
    dDay in month, from 1 to 31Day 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}}.