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.

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.

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

Last updated