Convert Datetime String to Required Format

Last updated on Jun 28, 2023
Definition: fromDateToFormattedDate(inputDate: datetime.date, requiredFormat: str) → str
Description: Converts a date and time to a format of your choice.
Parameters: - inputdate: The date in existing format.
- requiredFormat: The format to which the date object is must be converted. Example: MM/dd/yyyy hh:mm:ss a.
Returns: The date in the required string format.

The following script takes the date and time 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 #
    dateObject = properties['date'] # Date object from a source table
    requiredFormat = '<MMMM d, yy. hh:mm a>'
    properties['<formattedDate>'] = TimeUtils.fromDatetimeToFormattedDate( dateObject, requiredFormat )
    return event


Sample Transformation

The following script retrieves the value of the millis field, converts it to a datetime object using the fromEpochToDatetime method, and adds it as the previous_date field. Then, the function uses the fromDatetimeToFormattedDate method to transform the previous_date into the specified format and displays it in the new field, my_new_formatted_date.

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

def transform(event):

    # Get properties from the Event #
    previous_date = TimeUtils.fromEpochToDate(properties['millis'])
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['my_new_formatted_date'] = TimeUtils.fromDatetimeToFormattedDate( previous_date, requiredFormat )
    return event

Sample Output

The epoch time 1672987516206 is converted to January 6, 23. 12:00 AM and added in the my_new_formatted_date field.

Create Events



Revision History

Refer to the following table for the list of key updates made to this page:

Date Release Description of Change
Apr-03-2023 NA New document.

Tell us what went wrong