Converting Time String to Time Only

Hi every one,

when we enter in the UI for TimeString Property (Time = 6AM) it Should be store in the DB Like Time only Format (Time= 6:00:00 AM). Here the “Time” property Type is “Text”.

Can Anyone Help Me Regarding this.

Thank You in Advance,

Kiran G

@GandamallaK :warning: This is a GenAI-powered tool. All generated answers require validation against the provided references.

Based on your requirements, I’ve researched a few approaches to solve this issue. Since you’re using a Text property type (rather than a TimeOfDay or DateTime property), the most straightforward solution would be to use string manipulation in a Data Transform. Here’s how you can implement this:

Solution Using a Data Transform

  1. Create a Data Transform that will be triggered when the time value is entered in the UI
  2. In the Data Transform, use string manipulation functions to format the time string
  3. Here’s a step-by-step approach:

a. Extract the hour part and the AM/PM indicator from the input string
b. Add the “:00:00” part to create the full time format
c. Ensure proper spacing between the time and AM/PM

Sample Implementation:

// Assuming the input property is .TimeInput and the output property is .TimeFormatted
// Step 1: Check if the input contains "AM" or "PM"
When
  .TimeInput contains "AM" or .TimeInput contains "PM"
// Step 2: Extract the hour and AM/PM parts
Page-Property-Set .TempHour to @substring(.TimeInput, 1, @indexOf(.TimeInput, "AM", 1) - 1)
// If AM not found, try PM
When
  @indexOf(.TimeInput, "AM", 1) = 0
Page-Property-Set .TempHour to @substring(.TimeInput, 1, @indexOf(.TimeInput, "PM", 1) - 1)
Page-Property-Set .TempAMPM to @if(@indexOf(.TimeInput, "AM", 1) > 0, "AM", "PM")
// Step 3: Format the time string
Page-Property-Set .TimeFormatted to .TempHour + ":00:00 " + .TempAMPM

For more complex cases (like handling double-digit hours), you might need to add additional logic to the Data Transform.

Alternative Approach Using an Activity

If you prefer using an Activity, you can create one with Java steps to manipulate the string. This might be more flexible for complex formatting requirements:

  1. Create an Activity with an input parameter for the time string
  2. Add a Java step to format the string
  3. Return the formatted string as an output parameter

Sample Java Code:

String inputTime = tools.getParamValue("TimeInput");
String formattedTime;

 
// Extract hour and AM/PM
int amIndex = inputTime.indexOf("AM");
int pmIndex = inputTime.indexOf("PM");
String ampm;
int hourEndIndex;

 
if (amIndex > 0) {
    ampm = "AM";
    hourEndIndex = amIndex;
} else {
    ampm = "PM";
    hourEndIndex = pmIndex;
}

 
String hour = inputTime.substring(0, hourEndIndex);
formattedTime = hour + ":00:00 " + ampm;

 
tools.putParamValue("TimeFormatted", formattedTime);

Implementation Considerations

  1. When to Apply the Transformation: You can apply this transformation:
    • When the data is submitted from the UI (using a data transform in the flow action)
    • When the data is saved to the database (using a validate rule)
    • When the data is displayed (using a cell formatter in the UI)
  2. Best Practice: While using a Text property works for your current needs, consider if a TimeOfDay property type might be more appropriate for future requirements, as it provides built-in time handling capabilities.
  3. Validation: Add validation to ensure the input format is consistent before applying the transformation.

References: