Share

Parse Date String to Datetime Format

Definition: parseDateStringToDatetime (dateString: str, dateFormat: str) → datetime.datetime
Description: Parses the existing date format and returns a Python datetime object.
Parameters: - dateString: The date in string form. Example: 2023-03-02 13:30:30 PM.
- dateFormat: The format of the dateString parameter. Example: yyyy-MM-dd HH:mm:ss a.
Returns: The parsed value as a datetime object.

The following script takes in a date string in date format as input 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 #
    eventName = event.getEventName()
    properties = event.getProperties()
    dateString = '<date and timestamp value>'
    formatOfDate = '<input date format>'
    properties['<dateTimeObject>'] = TimeUtils.parseDateStringToDatetime( dateString, formatOfDate )
    return event


Sample Transformation

The following script transforms the date string in a date format into a datetime object by utilizing the parseDateStringToDatetime( dateString, formatOfDate ) method, and returns the result in the new_date_time_object 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()
    dateString = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    properties['new_date_time_object'] = TimeUtils.parseDateStringToDatetime( dateString, formatOfDate )
    return event

Sample Output

The date string 01/02/2019 09:00:00 AM is converted timestamp object with value 1546419600000 and added as a new_date_time_object field.

Create Events


Last updated on Jun 28, 2023

Tell us what went wrong

Skip to the section