# Working with Timestamps in R

The SF timestamp is formatted as UNIX timestamp with millisecond precision. For our R scripting engine, this looks like an ordinary numeric. To create a date object, use the following snippet:

```
as.POSIXct(timestamp/1000, origin = "1970-01-01", tz="Europe/Vienna")
```

To use a R date object outside of a script, define a script result variable with data type either "timestamp" or "string". Using "string" has the advantage of formatting the date-time object as you like but when using a timestamp the widget will adjust the representation to the displayed time frame. Use the following code snippets to correctly format the date-objects to meet the correct output format requirements:

String as output,

```
outputStringTimestamp = as.character(internalDateObject)
```

Timestamp as output,

```
outputTimestamp = as.numeric(internalDateObject) * 1000
```

Alternatively look at the [lubridate](https://www.rdocumentation.org/packages/lubridate/versions/1.7.4) package. It offers many functions for handling timestamps and datetime objects in very intuitive way, just remember that lubridate is also expecting a timestamp in seconds and not in milliseconds, so devide the timestamp you get from your data by 1000.


---

# Agent Instructions: 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://manual.senseforce.io/manual/sf-platform/script-editor/working-with-timestamps/working-with-timestamps-in-r.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.
