1

I want to convert a sObject query response parameter to a Time object which will be displayed in the front-end, and I am a bit confused since there is no method valueof() in the class Time in Apex like there is in the class Date.

I have used the below method to wrap a sObject to a Visit object.

public static List<Visit> getVisits(List<SObject> objs) {
    List<Visit> visits = new List<Visit>();
    for (SObject obj : objs) {
      Visit visit = new Visit();
        visit.visitDate=Date.valueOf(obj.get('Date__c'));
        Visit.visitTime= (Time)obj.get('Time__c');
        visit.patientEmail= String.valueOf(obj.get('Patient_Email__c'));
        visit.patientName=String.valueOf(obj.get('Patient_Name__c'));
        visit.visitDescription=String.valueOf(obj.get('Description__c'));
        visit.visitName=String.valueOf(obj.get('Name'));
      visits.add(visit);
    }

    return visits;
}

class Visit{
    @AuraEnabled
    public Date visitDate;

    @AuraEnabled
    public Time visitTime;

    
    @AuraEnabled
    public String patientEmail;
    
    
    @AuraEnabled
    public String patientName;

    @AuraEnabled
    public String visitDescription;

    @AuraEnabled
    public String visitName;
}

But unfortunately the Visit.visitTime= (Time)obj.get('Time__c'); line just converts it into gibberish number string in this format: 900000 Maybe there is some provided method that does this conversion.

1 Answer 1

3

There is no Time data type in Javascript, so you're getting the number of milliseconds (a timestamp).
Formatted Time component requires an ISO8601 formatted time string, so you could change the type of visitTime to String and then store it as
Visit.visitTime = String.valueOf((Time)obj.get('Time__c'));.
This way you'll get an ISO8601 time string, like 10:40:57.000Z.

Otherwise, if you cannot change the type of visitTime, you could use a Formatted Date Time component.
This component accepts a timestamp as value and if you want to display only the time, you have to set the following attributes: hour, minute and second.
I.E.

<lightning-formatted-date-time
    value="900000"
    hour="2-digit"
    minute="2-digit"
    second="2-digit">
</lightning-formatted-date-time>
1
  • Thank you! This is exactly what I needed! Apr 25, 2021 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.