4. Datetime
A datetime
type represents a single moment in time in a platform-independent format.
Postback Format
When performing a postback, the WEM runtime expects the datetime
to be in ISO date format. You can use the FormatDate()
function with the Iso8601
constant as the second argument to convert the datetime
to this format.
2025-12-31
2025-12-31 23:59
2025-12-31 23:59:59
2025-12-31 23:59:59.999999
An optional "T" can be included between the date and time parts, as in "2025-12-31T23:59". Additionally, you can add timezone-specific parts such as "Z", "+2:00", or "-5:00", as in "2025-12-31T23:59-5:00". Be aware that if you include a timezone part, the WEM runtime will convert the time to local time. For example, a datetime
of "2025-01-01T01:00:00-2:00" will be converted to "2025-01-01T03:00:00". You can test this with the text datetime input field widget below.
Text Datetime Input Field Example
This is straightforward. The only requirement is to convert the datetime
value of @DateTimeProperty
into the correct format using FormatDate(@DateTimeProperty, Iso8601)
.
Native Browser Datetime Input Field Example
This example is almost identical to the text datetime input field example. The only difference is that we set type="datetime-local"
on the input field. Since the browser adheres to the same datetime specifications, we can again use FormatDate(@DateTimeProperty, Iso8601)
.
Native Browser Date Input Field Example
Unfortunately, the browser is a bit stricter in this case. Since we do not need to consider time, we set type="date"
on the input field. However, we cannot use Iso8601
as the second argument for FormatDate()
. The reason is that the browser we tested does not accept a value that includes time. For instance, if you have value="2025-01-01T23:59:59"
, the entire value will be ignored. Fortunately, we can provide our own format in the FormatDate()
function, as demonstrated in the widget example above: FormatDate(@DateProperty, "yyyy-MM-dd")
.
JavaScript
Using a datetime
type within a JavaScript context is quite simple. By printing it with the JavaScript encode <?js
, you will receive a new Date()
object in return. For example, if we have a @DateTimeProperty
and we want to log the year from that datetime in JavaScript:
This will result in:
Last updated
Was this helpful?