# Working with Timestamps in Python

The SF timestamp is formatted as UNIX timestamp (UTC) with millisecond precision. For our Python scripting engine, this looks like an ordinary integer. To transform a single timestamp into a date object, use the following snippet:

```
import datetime as dt
from dateutil.tz import gettz
dt.datetime.fromtimestamp(timestamp / 1000).astimezone(gettz('Europe/Vienna'))
```

If you want to know more about datetime objects in Python, check out the [documentation](https://docs.python.org/3.6/library/datetime.html).

{% hint style="warning" %}
**Note,** that the above code does not work for a list of timestamps and depending on your use case you maybe want to use a different package for working with date-time.\
If you vectorize the above code, keep in mind that you will often have a lot of rows, so having a fast implementation here is the key to a well performing script.
{% endhint %}

To use a Python date object outside 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,

```
outputString = pythonDateTimeObj.strftime('%Y-%m-%d %H:%M:%S')
#or
outputString = pythonDateTimeObj.isoformat()
```

Timestamp as output,

```
outputTimestamp = pythonDateTimeObj.timestamp() * 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-python.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.
