Open In App

JavaScript Program to Convert Seconds to Milliseconds

Last Updated : 29 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Seconds and milliseconds are important time units for planning schedules and managing daily activities and performance optimizations. Understanding this conversion helps in better time management.

// Formula to convert second into millisecond :
1 second = 1 * 1000 millisecond

Below are the approaches for converting seconds into milliseconds in JavaScript:

Table of Content

Using Function

In this approach, the code defines a function, convertSecondsToMilliseconds(), that takes the seconds as an input parameter for converting it into milliseconds. It then invokes the function with a second value of 50. Inside the function, use the formula: seconds*1000, to convert it into milliseconds in JavaScript.

Example: The example below demonstrates converting seconds into milliseconds using a function.

JavaScript
function convertSecondsToMilliseconds(seconds) {
    return seconds * 1000;
}

const seconds = 50; 
const milliseconds = convertSecondsToMilliseconds(seconds);
console.log("Milliseconds:", milliseconds);

Output
Milliseconds: 50000

Time Complexity: O(1)

Space Complexity: O(1)

Using Class

In this approach, define a class named “TimeConverter” which will contain a method called convertToMilliseconds(), which will handle the conversion of seconds into milliseconds. Instantiate the TimeConverter() class using the new keyword, which creates an object of the class. Call the convertToMilliseconds() method on the converter object, passing the number of seconds you want to convert as an argument.

Example: The example below demonstrates converting seconds into milliseconds using class.

JavaScript
class TimeConverter {
    convertToMilliseconds(seconds) {
        return seconds * 1000;
    }
}

const converter = new TimeConverter();
const seconds = 50; 
const milliseconds = converter.convertToMilliseconds(seconds);
console.log("Milliseconds:", milliseconds);

Output
Milliseconds: 50000

Time Complexity: O(1)

Space Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads