# Converting Time String to Time Only

**URL:** <https://forums.pega.com/t/converting-time-string-to-time-only/9340>\
**Category:** User Experience\
**Tags:** 8-6, user-experience, pega-platform, case-management, system-architect\
**Created:** [April 10, 2025, 12:45pm UTC](https://forums.pega.com/t/converting-time-string-to-time-only/9340 "2025-04-10T12:45:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GandamallaK](https://avatars.discourse-cdn.com/v4/letter/g/ea5d25/32.png) [@GandamallaK](https://forums.pega.com/u/GandamallaK)\
**Post date:** [April 10, 2025, 12:45pm UTC](https://forums.pega.com/t/converting-time-string-to-time-only/9340/1 "2025-04-10T12:45:02Z")

</div>

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

 ![Sample Image.jpg](https://pega1.discourse-cdn.com/pega/original/2X/3/33fdcc4b74bbcaecdcba02b760a77ae00a59c8b4.jpeg)

---

<div class="post-metadata">

**Author:** ![MarijeSchillern](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/marijeschillern/32/23321_2.png) [@MarijeSchillern](https://forums.pega.com/u/MarijeSchillern)\
**Post date:** [April 25, 2025, 3:30pm UTC](https://forums.pega.com/t/converting-time-string-to-time-only/9340/2 "2025-04-25T15:30:05Z")

</div>

@GandamallaK ⚠ 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:**

```auto
// Assuming the input property is .TimeInput and the output property is .TimeFormatted

```

```auto
// Step 1: Check if the input contains "AM" or "PM"

```

```auto
When

```

```auto
  .TimeInput contains "AM" or .TimeInput contains "PM"

```

```auto
// Step 2: Extract the hour and AM/PM parts

```

```auto
Page-Property-Set .TempHour to @substring(.TimeInput, 1, @indexOf(.TimeInput, "AM", 1) - 1)

```

```auto
// If AM not found, try PM

```

```auto
When

```

```auto
  @indexOf(.TimeInput, "AM", 1) = 0

```

```auto
Page-Property-Set .TempHour to @substring(.TimeInput, 1, @indexOf(.TimeInput, "PM", 1) - 1)

```

```auto
Page-Property-Set .TempAMPM to @if(@indexOf(.TimeInput, "AM", 1) > 0, "AM", "PM")

```

```auto
// Step 3: Format the time string

```

```auto
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:**

```auto
String inputTime = tools.getParamValue("TimeInput");

```

```auto
String formattedTime;

```

```auto

 

```

```auto
// Extract hour and AM/PM

```

```auto
int amIndex = inputTime.indexOf("AM");

```

```auto
int pmIndex = inputTime.indexOf("PM");

```

```auto
String ampm;

```

```auto
int hourEndIndex;

```

```auto

 

```

```auto
if (amIndex > 0) {

```

```auto
    ampm = "AM";

```

```auto
    hourEndIndex = amIndex;

```

```auto
} else {

```

```auto
    ampm = "PM";

```

```auto
    hourEndIndex = pmIndex;

```

```auto
}

```

```auto

 

```

```auto
String hour = inputTime.substring(0, hourEndIndex);

```

```auto
formattedTime = hour + ":00:00 " + ampm;

```

```auto

 

```

```auto
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:

- [Pega Support: Text property to datetime formatting](https://support.pega.com/question/text-property-datetime-dateformat)
- [Pega Documentation: Date properties](https://docs.pega.com/bundle/platform/page/platform/user-experience/date-properties.html)
- [Pega Documentation: Best practices for Pega Infinity application development](https://docs.pega.com/bundle/platform/page/platform/app-dev/best-practices-infinity-application-development.html)
