0

in the query editor, I can see the time value as shown in the below image.

enter image description here

But when I return the same query to LWC it changes the time format to below

enter image description here

now if I use

<lightning-formatted-time value={value from apex}>

then instead of showing formatted time it shows integer values, could someone please help?

2 Answers 2

2

Formatted Time displays a read-only representation of time in the user's locale format. A valid ISO8601 formatted time string must be used.

So, this tag expects the time to be in a valid time string, but Apex would convert this to Millisecond value.

Instead you can use, Formatted Date Time to format the Long value and display in LWC.

Refer example below:

<lightning-formatted-date-time value="23402020" hour="2-digit" minute="2-digit" second="2-digit" hour12="false" time-zone="UTC">
</lightning-formatted-date-time>

The above code will display value as 06:30:02 AM

3
  • Thank you for your answer, could you please help me with how to convert 00:00:00.000 to the format getting from apex?
    – gs650x
    Jul 5, 2021 at 13:21
  • Can you elaborate your query, probably by adding a new question, with details on where you are stuck? Jul 5, 2021 at 13:31
  • like from apex I am getting 27002020 and from input field of time if I select 6:30PM I want to convert it 27002020 instead of 6:30:00:00
    – gs650x
    Jul 5, 2021 at 13:32
0

When i am working on a same scenario where i accessing the time from apex and console it into js it get converted into miliseconds.

to convert this milisecond time into the proper time format u need to use js functions i mentioned below:

 connectedCallback() {
    this.handleDataReceived(37800000);   // i passing parameter while calling method u can pass ur variable that containing milisecond time format in it.

}

// Example usage:
handleDataReceived(milliseconds) {
    const formattedTime = this.convertMillisecondsToTime(milliseconds);
    console.log(formattedTime); // Output to console
    // Further processing or UI updates can be done here
}

convertMillisecondsToTime(milliseconds) {
    // Create a new Date object using milliseconds
    const date = new Date(milliseconds);

    // Extract hours, minutes, and seconds
    const hours = date.getUTCHours();
    const minutes = date.getUTCMinutes();
    const seconds = date.getUTCSeconds();

    // Format the time string
    const timeString = hours.toString().padStart(2, '0') + ':' +
        minutes.toString().padStart(2, '0') + ':' +
        seconds.toString().padStart(2, '0');

    return timeString;
}

Here Is The Output Of Above Code

Hope it helps you.

1
  • Doesn't your Javascript produce a time string representing UTC and (probably, if you don't live in GMT) not the time of the user?! The lightning-formatted-time expects the time to be user time. Feb 9 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .