TypeError: date.getTime is not a function in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# TypeError: date.getTime is not a function in JavaScript

The "TypeError: date.getTime is not a function" error occurs when the getTime() method is called on a value that is not a date object.

To solve the error, convert the value to a date before calling the method or make sure to only call the getTime() method on valid date objects.

typeerror date getTime is not a function

Here is an example of how the error occurs.

index.js
const date = Date.now(); console.log(date); // ๐Ÿ‘‰๏ธ 1639... // โ›”๏ธ TypeError: date.getTime is not a function const result = date.getTime();

We called the Date.now() function and got an integer back. We then called the getTime() method on the integer, which caused the error.

# Only call the getTime() method on valid Date objects

To solve the error, make sure to only call the getTime() method on valid date objects.

index.js
const t1 = new Date().getTime(); console.log(t1); // ๐Ÿ‘‰๏ธ 1639... const t2 = new Date('Sept 24, 22 13:20:18').getTime(); console.log(t2); // ๐Ÿ‘‰๏ธ 166401...

only call gettime method on valid date objects

The code for this article is available on GitHub

You can get a date object by passing a valid date to the Date() constructor.

Note that if an invalid date is passed to the Date() constructor, you would get an NaN (not a number) value back.

index.js
const t1 = new Date('invalid').getTime(); console.log(t1); // ๐Ÿ‘‰๏ธ NaN

You can console.log the value you are calling the getTime method on and see if it's a valid Date object.

# Check if the value is a Date before calling getTime()

You could conditionally check if the value is a Date object in the following way.

index.js
const d1 = new Date(); if (typeof d1 === 'object' && d1 !== null && 'getTime' in d1) { const result = d1.getTime(); console.log(result); // ๐Ÿ‘‰๏ธ 163966... }

check if value is date before calling gettime

The code for this article is available on GitHub

Our if statement uses the logical AND (&&) operator. So for the if block to run, all conditions have to be met.

We first check if the d1 variable stores a value with a type of object because dates have a type of object.

Then we check if the variable is not equal to null. Unfortunately, if you check the type of null with console.log(typeof null), you will get an "object" value back, so we have to make sure the value is not null.

The last thing we check for is that the object contains a getTime property.

If all the conditions are met, we can safely call the getTime method on the object.

This approach is called duck-typing.

When using duck-typing, we simply check if the object implements specific properties or methods and if it does, we assume it's an object of the correct type.

The getTime() method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.

index.js
const utcTimestamp = new Date().getTime(); console.log(utcTimestamp); // ๐Ÿ‘‰๏ธ 1672484720306

You can use the method to assign a date and time to another Date object.

index.js
const date = new Date(); console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-02T17:58:14.011Z const dateCopy = new Date(); dateCopy.setTime(date.getTime()); console.log(dateCopy); // ๐Ÿ‘‰๏ธ 2024-03-02T17:58:14.011Z

assigning date and time to another date object

The code for this article is available on GitHub
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev