function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shuhbam SinhaShuhbam Sinha 

How to conver Milisecods to Days Hours, Minutes and Seconds in LWC

Hello Everyone ,
I need to convert MS to Dasy:hh:mm:ss in lwc. I tried below code it is givng me days in decimal which I dnot want. Can anyone please help me on the same.
msToTime(s) {
        var ms = s % 1000;      
        s = (s - ms) / 1000;
        var secs = s % 60;

        s = (s - secs) / 60;
        var mins = s % 60;

        s= (s - mins) / 60;
        var hrs = s % 60;
        var hrs = (s - mins) / 60;

        var days = (s-hrs) / 24;

        var timer = '';
        if(days && days>0){
            timer = days +' days ';
        }
        
        if(hrs && hrs>0){
            timer = timer + hrs +' hr ';
        }
        if(mins && mins>0){
            timer = timer + mins +' min ';
        }
        if(secs && secs>0){
            timer = timer + secs +' sec ';
        }
        return timer;
      }
}

 
SubratSubrat (Salesforce Developers) 
Hello Shubham ,

To convert milliseconds to days, hours, minutes, and seconds in LWC, you can go through these articles and also try with below provided code .

References -> 
msToTime(s) {
    var ms = s % 1000;      
    s = (s - ms) / 1000;
    var secs = s % 60;

    s = (s - secs) / 60;
    var mins = s % 60;

    s= (s - mins) / 60;
    var hrs = s % 24;

    var days = (s - hrs) / 24;

    var timer = '';
    if(days && days>0){
        timer = days +' days ';
    }
    
    if(hrs && hrs>0){
        timer = timer + hrs +' hr ';
    }
    if(mins && mins>0){
        timer = timer + mins +' min ';
    }
    if(secs && secs>0){
        timer = timer + secs +' sec ';
    }
    return timer.trim();
}

If the above inforamtion helps , please mark this as Best Answer.
Thank you.
Prateek Prasoon 25Prateek Prasoon 25
Text
      
    
    
    
      import { LightningElement, api } from 'lwc';
export default class TimeDuration extends LightningElement {
    @api durationInMilliseconds
    get durationInDays() {
        const days = Math.floor(this.durationInMilliseconds / (1000 * 60 * 60 * 24));
        return days;
    }
    get durationInHours() {
        const hours = Math.floor((this.durationInMilliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        return hours;
    }
    get durationInMinutes() {
        const minutes = Math.floor((this.durationInMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
        return minutes;
    }
    get durationInSeconds() {
        const seconds = Math.floor((this.durationInMilliseconds % (1000 * 60)) / 1000);
        return seconds;
    }
}

If you find my answer helpful, please mark it as the best answer. Thanks