/
Working with Timestamps in Python

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

Related content

Working with Timestamps in R
Working with Timestamps in R
More like this
Time Zones
More like this
FAQ and Troubleshooting
FAQ and Troubleshooting
More like this
FAQ and Troubleshooting - Dashboards
FAQ and Troubleshooting - Dashboards
More like this
Applying zoom to global timestamp filter
Applying zoom to global timestamp filter
More like this
Details of the Script Editor
Details of the Script Editor
More like this