4. Datetime

A datetime type represents a single moment in time in a platform-independent format.

<?
	var @d: datetime
	@d := Date(1643, 1, 4)
	@d := '1643-01-04'
	@d := '1643-01-04T13:03:34'
?>

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.

Valid Formats

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

<? register input @DateTimeProperty ?>
<label>
	Current date: <?= @DateTimeProperty ?>
	<input type="text" name="<?attr OutputId(@DateTimeProperty) ?>" value="<?attr FormatDate(@DateTimeProperty, Iso8601) ?>">
</label>

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

<? register input @DateTimeProperty ?>
<label>
	Select date and time:
	<input type="datetime-local" name="<?attr OutputId(@DateTimeProperty) ?>" value="<?attr FormatDate(@DateTimeProperty, Iso8601) ?>">
</label>

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

<? register input @DateProperty ?>
<label>
	Select date:
	<input type="date" name="<?attr OutputId(@DateProperty) ?>" value="<?attr FormatDate(@DateProperty, "yyyy-MM-dd") ?>">
</label>

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:

<? startupscript ?>
    const d = <?js @DateTimeProperty ?>;
    console.log(d.getFullYear());
<? end ?>

This will result in:

<? startupscript ?>
    const d = new Date(2025, 2, 24, 16, 56, 37, 42);
    console.log(d.getFullYear());
<? end ?>

Last updated

Was this helpful?