• Node.js Video Tutorials

Node.js - os.uptime() Method



The os module of Node.js provides a bunch of operating system-related utility methods and properties.

The Node.js os.uptime() method returns an integer that has the system uptime in number of milliseconds. The value returned by this method will be bigger than zero if the process is running, and will be equal to zero if it's not running or hasn't been initialized yet.

Syntax

Following is the syntax of the Node.js os.uptime() method

os.uptime()

Parameters

This method does not accept any parameters.

Return value

This method returns an integer value that represents the system uptime in number of seconds.

Example

In the following example, we are trying to import the os module and return the uptime of the current system.

const os = require('os');
console.log(os.uptime());

Output

After executing the above program, the os.uptime() method will return the system running time in seconds.

35966

Example

In the following example, we tried to print the uptime of the current system in seconds, minutes, hours, and days.

const os = require('os');
var Hours = os.uptime()/(60*60);
console.log('Up time in (Seconds): '+ os.uptime() + ' Seconds');
console.log('Up time in (Minutes): '+ os.uptime()/60 + ' Minutes');
console.log('Up time in (Hours): '+ Hours + ' Hours');
var days = Hours/(24);
console.log('Up time in (Days): '+ days + ' Days');

Output

After executing the above program, we printed the system's running time in seconds, minutes, hours, and days.

Up time in (Seconds): 35936 Seconds
Up time in (Minutes): 598.9333333333333 Minutes
Up time in (Hours): 9.982222222222223 Hours
Up time in (Days): 0.415925925925926 Days
nodejs_os_module.htm
Advertisements