> For the complete documentation index, see [llms.txt](https://docs.wem.io/platform/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/4-datetime.md).

# 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 ?>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/4-datetime.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
