What is setUTCDate() in Javascript?

The setUTCDate function in JavaSript is a method of the Date class which is used to set the value of day following the Universal Time Coordinated (UTC) time.

Parameters

Date.setUTCDate(newDayValue)

The function takes in one parameter that is an integer.

In the above snippet of code, newDayValue is an integer with an expected value between 1 and 31.

If a number greater than 31 or lesser than 0 is passed, the month’s value is adjusted accordingly.

Return value

The total number of milliseconds from 1 January 1970 00:00:00 UTC and the updated date is returned.

Example

In the code below, an unexpected value is passed to the setUTCDate function. Entering a negative number makes a date go back by the specified number of days:

// an object of Date type is created to apply the function on
var obj = new Date('November 1, 2001 09:24:21');
// Date is printed in DAY:MONTH:YEAR format in UTC
console.log("DATE BEFORE FUNCTION CALL: " + obj.getUTCDate() + " : " + obj.getUTCMonth() + " : " + obj.getUTCFullYear())
// setUTCDate function is called with an unexpected value.
// It'll update the value of the month accordingly
obj.setUTCDate(-10);
console.log("DATE AFTER FUNCTION CALL: " + obj.getUTCDate() + " : " + obj.getUTCMonth() + " : " + obj.getUTCFullYear())

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved