Share

Parse Date String to Time

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

The following script takes a date string in the specified date format and returns a time 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['<timeObject>'] = TimeUtils.parseDateStringToTime( dateString, formatOfDate )
    return event


Sample Transformation

The following script transforms the date string in a specified date format into a time object by utilizing the parseDateStringToTime( dateString, formatOfDate ) method, and returns the result in the new_parsed_time 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_parsed_time'] = TimeUtils.parseDateStringToTime( dateString, formatOfDate )
    return event

Sample Output

The date string 01/02/2019 09:00:00 AM is converted to 09:00:00 and added in the new_time_object field.

Create Events


Last updated on Jun 28, 2023

Tell us what went wrong

Skip to the section