Solution To Fix “TypeError: toLocaleDateString is not a function” Error in JavaScript

TypeError: toLocaleDateString is not a function in JavaScript

The “TypeError: toLocaleDateString is not a function” in JavaScript is not difficult to fix. Here are some examples to help you understand this error and develop a reasonable solution.

The cause of the TypeError: toLocaleDateString is not a function in JavaScript

The “TypeError: toLocaleDateString is not a function” in JavaScript happens when you use an object to call a method that is not a date object or is syntactically incorrect.

The error message occurs as follows:

Uncaught TypeError TypeError: num.toLocaleDateString is not a function

Example:

var num = 14387912987987;
var str = "Hello world!!";
var date = new Date();

var dateStringByNum = num.toLocaleDateString(); // The error appears in here
var dateStringByStr = str.toLocaleDateString(); // The error appears in here
var dateStringByDate = date.toLocaleDateString(); // The error appears in here

How to fix this error?

Before coming to some ways to fix the above error, we must know the correct syntax of the toLocaleDateString() method to avoid the program code being syntactically incorrect and throwing errors.

Syntax:

Date.toLocaleDateString()

After you use this syntax, if your program doesn’t have an error, congratulations on the error having been fixed, if not, please continue with this article with me.

Check if the object has a toLocaleDateString() method

For a method to work with an object, the method itself must be present in the object. Therefore, in this way, I will create a function to check if the variable we are using is the object that has the output and check whether the toLocaleDateString() method is inside or not.

Example:

function checkHastoLocaleDateString(variable) {
	// Check if there is The toLocaleDateString() method
	if (variable !== null && typeof variable == "object" && 'toLocaleDateString' in variable) {
		console.log(variable.toLocaleDateString())
	} else {
		console.log("The toLocaleDateString() method cannot be used")
	}
}

checkHastoLocaleDateString(num);
checkHastoLocaleDateString(str);
checkHastoLocaleDateString(date);

Output:

The toLocaleDateString() method cannot be used
The toLocaleDateString() method cannot be used
11/10/2022

With this method, the program will no longer have an error because if the passed variable fails, the program will be converted into a message string. If the passed variable has the above method, your program will run that method and output the result.

Call the function after creating a new date from the variable you want to call

We can use this way to solve this problem succinctly.

Example:

var num = 14387912987987;
var str = "Hello world!!";
var date = new Date();

var dateStringByNum = new Date(num).toLocaleDateString(); // 12/8/2425
console.log(dateStringByNum);

var dateStringByStr = new Date(str).toLocaleDateString(); // Invalid Date 
console.log(dateStringByStr);

Output:

12/8/2425
Invalid Date

We see from the variable num we get a number and use it to create a new date. With the function, we will get a date as the output date, and with the variable str not a date format string, the generated date is invalid and should return the string “Invalid Date”.

In this way, you can’t use it when the object is a date, so try the variable with the toLocaleDateString() method before using it. If it doesn’t work, then try this.

Summary

I hope the above methods will help you fix the TypeError: toLocaleDateString is not a function in JavaScript. Let’s try them to get your desired results. Thanks for reading!

Maybe you are interested:

Leave a Reply

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