[Solved] TypeError: toISOString is not a function

Total
0
Shares

If we call the toISOString() method on the value that is not of a Date object, JavaScript will throw a TypeError: toISOString is not a function.

In this tutorial, we will look at what is TypeErrror: toISOString is not a function error and how to resolve them with examples.

What is TypeError: toISOString is not a function error?

Let us take a simple example to demonstrate this issue

// Declare and store the data into a variable
const date= Date.now();

// Prints the UNIX epoch
console.log(date);

// get the Date as ISO Format String
const output = date.toISOString();

Output

1655113057893
TypeError: date.toISOString is not a function

In the above example, we have declared a variable and we have stored the UNIX epoch timestamp as the integer value into it. The Date.now() method returns the UNIX timestamp which is of type number.

In the next statement, we call the Date.prototype.toISOString() method on the value of the type number, and hence we get a TypeError: toISOString is not a function.

We can also check the variable type using typeof() to confirm the datatype.

// Declare and store the data into a variable
const currDate = Date.now();

// Prints the UNIX epoch
console.log(currDate);

console.log("The type of variable is",typeof currDate)

Output

1655113670272
The type of variable is number

How to fix TypeError: toISOString is not a function error?

The Date.prototype.toISOString() method can only be used on the Date object and not on any other object type. 

There are two ways to fix this issue in JavaScript.

Solution 1: Convert the value into a Date Object

We can easily resolve the issue by converting the value into a Date object before calling the toISOString() method.

If we know the value can be converted to a valid Date object, then we can use the Date() constructor in JavaScript that returns the Date object.

Let us take an example to resolve the issue using the Date() constructor.

// Declare and store the data into a variable
const currDate = Date.now();

// Prints the UNIX epoch
console.log("Unix time stamp of current date", currDate);

// Converts timestamp into Date Object
const dt = new Date(currDate)

// Print the Date as a ISO Format string
console.log(dt.toISOString())

Output

Unix time stamp of current date 1655571942225
2022-06-18T17:05:42.225Z
If the Date is invalid

If called with an invalid date string, or if the date to be constructed will have a UNIX timestamp less than -8,640,000,000,000,000 or greater than 8,640,000,000,000,000 milliseconds, it returns an Invalid Date and calling the toISOString() method on this will return RangeError: Invalid time value.

// Declare and store the data into a variable
const currDate = "Hello World";

// Converts date like object into Date Object
const dt = new Date(currDate)

// Print the Date as ISO Format string
console.log(dt.toISOString())

Output

RangeError: Invalid time value

Solution 2 – Performing the type check

We can also perform a type check on the variable to check if it’s a valid date object and has it has the property of type toISOString before calling the toISOString() method.

There are 3 logical expression that we need to evaluate.

  • First we need to check if the variable stores a value which of type object
  • Once the first condition passes we need to check if the object is not null
  • Last we need to ensure if the object contains toISOString property.

Example – Type check using if/else

// Declare and store the data into a variable
const currDate = "2010/05/18 20:30:45";

// Converts date like object into Date Object
const dt = new Date(currDate)

if (typeof dt === 'object' && dt !== null && 'toISOString' in dt) {

    console.log("The data type is", typeof dt)

    // Print the Date as ISO Format String
    console.log(dt.toISOString())

}

else {
    console.log("Invalid Date Object")
}

Output

The data type is object
2010-05-18T15:00:45.000Z

Conclusion

The TypeError: toISOString is not a function occurs if we call a toISOString() method on the object that is not of type Date object. We can resolve the issue by converting the value into Date Object using Date() constructor before calling the toISOString() method or by performing a type check using typeof to see if the object is of type toISOString.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript String slice()

JavaScript String slice()

Table of Contents Hide slice() Exampleslice() Syntaxslice() Parameterslice() Return ValueExample 1: Using JavaScript String slice() methodExample 2: Using slice() method with negative indices In this tutorial, we will learn about…
View Post
JavaScript String endsWith()

JavaScript String endsWith()

Table of Contents Hide endsWith() SyntaxendsWith() ParametersendsWith() Return ValueJavaScript String endsWith() examplesJavaScript endsWith() example with length parameter In JavaScript, the endsWith() method determines whether the given string ends with a…
View Post
JavaScript String padEnd()

JavaScript String padEnd()

Table of Contents Hide padEnd() ExamplepadEnd() SyntaxpadEnd() ParameterpadEnd() Return ValueExample 1 – How padEnd() method works in JavaScript?Example 2 – Working of padEnd() method with default value In this tutorial,…
View Post