Share

Convert Epoch Time to a Datetime

Definition: fromEpochToDatetime(epoch: int) → datetime.datetime
Description: Converts an epoch value in milliseconds to a Python datetime object in timestamp format.
In this conversion, the value remains the same; only the data type is changed.
Parameters: epoch: The timestamp value in milliseconds to convert.
Returns: The converted value as a datetime object.

The following script takes an epoch value as an argument and returns a datetime object.

from io.hevo.api import Event
from io.hevo.api import TimeUtils


def transform(event):

    # Get properties from the Event #
    <epoch> = <milliseconds_value>
    <date_object> = TimeUtils.fromEpochToDatetime(<epoch>)
    return <newEvent>

Sample Transformation

The following script transforms the epoch time (in milliseconds) stored in the millis field into a datetime object by utilizing the fromEpochToDatetime(int: epoch) method, and returns the result in the newdatetimeobject field.

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):

    # Get properties from the Event #
    eventName = event.getEventName()
    properties = event.getProperties()
    epoch = properties["millis"]
    properties['newdatetimeObject'] = TimeUtils.fromEpochToDatetime(epoch)
    return event

Sample Output

The epoch time 1672986820000 is converted to a datetime object with the same value and displayed in the newdatetimeObject field.

Create Events


Last updated on Jun 30, 2023

Tell us what went wrong

Skip to the section