Warning: session_start(): open(/tmp/sess_7d4e21e2b752ff78b9d6ce7f61ae2e5e, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Add Seconds To A Date Using JavaScript - LearnShareIT

How To Add Seconds To A Date Using JavaScript

Add Seconds to a Date using JavaScript

Date is a class related to time in JS. To add seconds to a date using JavaScript, we have several methods that will be covered in this article. Let’s find out together!

Add seconds to a date using JavaScript

As a reminder, the Date class represents the number of milliseconds that have passed since ECMAScript‘s epoch (Which is January 1st 1970 or 1970-01-01 00:00:00:00 UTC+00) in your computer’s set timezone.

So the general idea for this is to get the specified Date’s time and add 1000*x milliseconds (x seconds), then set that as the Date’s new time. To do that, you can use the .getSeconds() and .setSeconds() or .getTime() and .setTime() functions of the Date class.

One thing to note, as mentioned above: The Date class will return the time in your computer’s set timezone; either you specify it differently by code, or pass a date value in it, in which its time will be set as specified or defaults to 00:00:00.

Using Date.prototype.getSeconds() and Date.prototype.setSeconds()

The .getSeconds() function gets the Date’s current second and ranges from 0 to 59.

The .setSeconds() function, as its name implies, sets the Date’s current second. If the specified second surpasses 59, it will up the minutes by the number of minutes the specified second amounts to and sets the current second to the remainder.

With the combination of the two functions, we can add seconds to the Date by first getting the current second and adding it by a specified amount. As such:

Code:

const date = new Date("2022-01-01 00:00:00");

function addSeconds(date, seconds) {
    date.setSeconds(date.getSeconds() + seconds);
}

console.log(date);

// Add 10 Seconds
addSeconds(date, 10); 
console.log(date);

// Add 2 Minutes (120 Seconds)
addSeconds(date, 120); 
console.log(date);

// Add 2 Minutes and 50 Seconds (170 Seconds)
addSeconds(date, 170); 
console.log(date);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:00:10 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:02:10 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:05:00 GMT+0700 (Indochina Time)

Additionally, if you want to keep the old variable as the .setSeconds() function does change the value, you can do something like this:

Code:

const date = new Date("2022-01-01 00:00:00");

function newDateAddSeconds(date, seconds) {
	const newDate = new Date(date.toISOString());
	newDate.setSeconds(date.getSeconds() + seconds);
	return newDate;
}

// Add 2 Minutes (120 Seconds)
const newDate = newDateAddSeconds(date, 120);

console.log(date);
console.log(newDate);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:02:00 GMT+0700 (Indochina Time)

Using Date.prototype.getTime() and Date.prototype.setTime()

The .getTime() function is pretty misleading as it doesn’t return the date like the .getISOString() function does. But instead returns the number of milliseconds, it has elapsed since the ECMAScript’s epoch.

The .setTime() function on the other hand works similarly to .setSeconds(). But in this case you would have to add 1000 milliseconds as this sets the total milliseconds and not the seconds.

Using these functions would work exactly the same as the code above but with some syntax changes. For example:

Code:

const date = new Date("2022-01-01 00:00:00");

function addSeconds(date, seconds) {
	date.setTime(date.getTime() + seconds * 1000);
}

console.log(date);

// Add 10 Seconds
addSeconds(date, 10);
console.log(date);

// Add 2 Minutes (120 Seconds)
addSeconds(date, 120);
console.log(date);

// Add 2 Minutes and 50 Seconds (170 Seconds)
addSeconds(date, 170);
console.log(date);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:00:10 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:02:10 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:05:00 GMT+0700 (Indochina Time)

Code:

const date = new Date("2022-01-01 00:00:00");

function newDateAddSeconds(date, seconds) {
	const newDate = new Date(date.toISOString());
	newDate.setTime(date.getTime() + seconds * 1000);
	return newDate;
}

// Add 2 Minutes (120 Seconds)
const newDate = newDateAddSeconds(date, 120);

console.log(date);
console.log(newDate);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jan 01 2022 00:02:00 GMT+0700 (Indochina Time)

Sidenote on the Date class’s UTC functions

UTC or Coordinated Universal Time describes timezones depending on each timezone’s longitude.

Although in this context the functions perform exactly like their normal counterparts but in UTC+00 and ignoring your computer’s set time zone. The UTC functions are:

  • .getUTCseconds()
  • .setUTCseconds()
  • .getUTCminutes()

These should only be used if you’re already dealing with a UTC+00 Date, otherwise stick to the normal versions for consistency with your computer’s set timezone. The ways to do the same but in other timezones will not be covered here.

Summary

To add seconds to a date using JavaScript, you must first get the specified Date’s current time by using either .getSeconds() or .getDate() and adding seconds or milliseconds respectively. Then set the new time with either .setSeconds() or .setDate(). Good luck to you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *