The Wayback Machine - https://web.archive.org/web/20240119132713/https://www.geeksforgeeks.org/javascript-date-settime-function/
Open In App
Related Articles

JavaScript Date setTime() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The JavaScript Date setTime() Function is a built-in Function in Javascript that is used to get a date object by adding given milliseconds to the date 01/01/1970

Syntax:

date.setTime(milliseconds)

Parameters: setTime() Function takes parameters as follows.

  • milliseconds: Number of milliseconds to add/subtract to the given date

Return Value: The setTime() Function returns milliseconds between January 1 1970 and the time you passed in the parameter.

Example 1: Adding 300000000000 milliseconds to 01/01/1970

Javascript




<script>
    // Initialization
    let date = new Date();
      
    // Execution
    let output = date.setTime(300000000000);
      
    // Display Output
    console.log(`setTime() Output:`, output, date);
</script>


Output:

setTime() Output: 300000000000 Thu Jul 05 1979 10:50:00 GMT+0530 (India Standard Time)

Example 2: Subtracting 300000000000 milliseconds to 01/01/1970.

Javascript




<script>
    // Initialization
    let date = new Date();
      
    // Execution
    let output = date.setTime(-300000000000);
      
    // Display Output
    console.log(`setTime() Output:`, output, date);
</script>


Output:

setTime() Output: -300000000000 Thu Jun 30 1960 00:10:00 GMT+0530 (India Standard Time)

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.

Supported Browsers: The browsers supported by the JavaScript Date setTime() Function are listed below: 

  • Google Chrome 1 and above
  • Internet Explorer 3  and above
  • Mozilla Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 01 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials