Share

Convert Date String to Required Format

Definition: changeDateFormat(dateString: str, formatOfDate: str, requiredFormat) → str
Description: Converts a date string in one format to another format. For example, ISO to Timestamp.
The value is ingested in Hevo as a string. However, it is loaded to the Destination as a timestamp object, as indicated by the timestamp datatype in the Transformation output.
Note: The attributes of a time object include: hour, minute, and second. Example: 09:15:36
Parameters: - dateString: The date in string form. Example: 2023-03-02 13:30:30 PM.
- dateFormat: The existing format of the dateString argument. Example: yyyy-MM-dd HH:mm:ss a.
- requiredFormat: The format to which the date string must be converted. Example: MM/dd/yyyy hh:mm:ss a.
Returns: The date string in the required format.

The following script takes a date value as an argument and returns the date in the specified format.

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


def transform(event):

    # Get properties from the Event #
    dateString = '<date value>'
    formatOfDate = '<input date format>'
    requiredFormat = '<required output date format>'
    properties['<formattedDate>'] = TimeUtils.changeDateFormat(dateString, formatOfDate, requiredFormat)
    return event


Sample Transformation

The following script transforms the date in the dateString field into a varchar object by utilizing the TimeUtils.changeDateFormat(dateString, formatOfDate, requiredFormat) method, and returns the result in the newFormattedDate field.

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

def transform(event):

    # Get properties from the Event #
    dateString = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['newFormattedDate'] = TimeUtils.changeDateFormat(dateString, formatOfDate, requiredFormat)
    return event

Sample Output

The input date value 01/02/2019 09:00:00 AM is converted to January 2, 19. 09:00 AM, and added in the newFormattedDate field.

Create Events


Last updated on Oct 30, 2023

Tell us what went wrong

Skip to the section