How to set current time to some other time in JavaScript?

You cannot directly modify the system time with JavaScript since it uses the system's clock through the Date object. However, you can simulate different times by adjusting timezones or creating custom date objects with specific values.

Method 1: Using Timezone Conversion

You can display time for different timezones by calculating the offset from UTC:

<!DOCTYPE html>
<html>
<body>
    <script>
        var date, utc, offset, singaporeTime;
        date = new Date();
        
        document.write("Current Local Time: " + date + "<br>");
        
        // Get UTC time in milliseconds
        utc = date.getTime() + (date.getTimezoneOffset() * 60000);
        
        // Singapore is GMT+8
        offset = 8;
        singaporeTime = new Date(utc + (3600000 * offset));
        
        document.write("Singapore Time: " + singaporeTime.toLocaleString() + "<br>");
        
        // Tokyo is GMT+9
        var tokyoTime = new Date(utc + (3600000 * 9));
        document.write("Tokyo Time: " + tokyoTime.toLocaleString());
    </script>
</body>
</html>

Method 2: Creating Custom Date Objects

You can create Date objects with specific values to simulate any time:

<!DOCTYPE html>
<html>
<body>
    <script>
        // Current time
        var currentTime = new Date();
        document.write("Current Time: " + currentTime + "<br>");
        
        // Set a specific time (year, month-1, day, hour, minute, second)
        var customTime = new Date(2024, 11, 25, 14, 30, 0); // Dec 25, 2024 2:30 PM
        document.write("Custom Time: " + customTime + "<br>");
        
        // Add 2 hours to current time
        var futureTime = new Date(currentTime.getTime() + (2 * 60 * 60 * 1000));
        document.write("2 Hours Later: " + futureTime);
    </script>
</body>
</html>

Method 3: Using toLocaleString() with TimeZone

Modern JavaScript provides built-in timezone support:

<!DOCTYPE html>
<html>
<body>
    <script>
        var now = new Date();
        
        document.write("Local Time: " + now.toLocaleString() + "<br>");
        document.write("New York: " + now.toLocaleString("en-US", {timeZone: "America/New_York"}) + "<br>");
        document.write("London: " + now.toLocaleString("en-US", {timeZone: "Europe/London"}) + "<br>");
        document.write("Tokyo: " + now.toLocaleString("en-US", {timeZone: "Asia/Tokyo"}));
    </script>
</body>
</html>

Comparison

Method Use Case Browser Support
Manual Offset Calculation Basic timezone conversion All browsers
Custom Date Objects Simulating specific times All browsers
toLocaleString() with timeZone Multiple timezone display Modern browsers

Conclusion

While you cannot change the system time, JavaScript provides multiple ways to work with different times and timezones. Use timezone conversion for displaying times across regions and custom Date objects for simulating specific moments.

Updated on: 2026-03-15T23:18:59+05:30

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements