Skip to main content

How to Resolve "TypeError: date.getTime is not a function" Error in JavaScript

The TypeError: date.getTime is not a function is a common error in JavaScript that occurs when you try to call the .getTime() method on a value that is not a Date object. This typically happens when you are working with a variable that you think is a Date object but is actually a number, a string, null, or undefined.

This guide will explain the fundamental reason this error occurs, cover the most common mistake involving Date.now(), and teach you how to write defensive code to prevent this error.

The Core Problem: .getTime() Belongs to Date Objects

The .getTime() method is a function that exists exclusively on instances of the Date object. Its purpose is to return the number of milliseconds since the Unix Epoch (January 1, 1970).

The error is the JavaScript engine telling you, "You asked me to call the .getTime() function, but the variable you gave me isn't a Date object, so that function doesn't exist on it."

Example of problem:

// This is a number, not a Date object.
let notADate = 1672531200000;

// PROBLEM: Numbers do not have a `.getTime()` method.
notADate.getTime();

Error Output:

Uncaught TypeError: notADate.getTime is not a function

Cause 1 (Most Common): Date.now() vs. new Date()

This is the most frequent source of this error. Developers often confuse these two related but distinct Date functionalities.

  • new Date(): This is a constructor. It creates and returns a new Date object.
  • Date.now(): This is a static method. It returns a Number representing the current timestamp in milliseconds.

Example of problem:

// PROBLEM: `Date.now()` returns a number, not a Date object.
let timestampAsNumber = Date.now();

console.log(typeof timestampAsNumber); // Output: 'number'

// This line will throw the error.
timestampAsNumber.getTime();

Solution: if you need a Date object to work with, you must use the new Date() constructor.

// Correct: `new Date()` creates a Date object.
let dateObject = new Date();

console.log(typeof dateObject); // Output: 'object'

// Now you can safely call `.getTime()` on it.
let timestamp = dateObject.getTime();
console.log(timestamp);

Cause 2: The Variable is Not a Date Object

The error can also occur if your variable holds null, undefined, a string, or any other type that is not a Date object. This often happens when a function that was expected to return a Date fails and returns null instead.

Example of problem:

function getDateFromAPI() {
// Imagine this function fails and returns null
return null;
}

let myDate = getDateFromAPI(); // `myDate` is now `null`

// This will throw "TypeError: Cannot read properties of null (reading 'getTime')"
myDate.getTime();

The Solution: How to Validate Before Calling .getTime()

To prevent this error, you should write "defensive" code. Before you call .getTime(), you must verify that your variable actually holds a valid Date object.

Solution: this simple and reusable function is the best practice for validating a Date object.

/**
* Checks if the provided value is a valid Date object.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a valid Date.
*/
function isValidDate(value) {
// 1. Check if it's an instance of a Date.
// 2. Check if the date's time value is not NaN (to exclude "Invalid Date").
return value instanceof Date && !isNaN(value.getTime());
}

// Example Usage:
let date1 = new Date();
let date2 = Date.now(); // This is a number
let date3 = new Date('not-a-date'); // This is an "Invalid Date"
let date4 = null;

console.log(isValidDate(date1)); // Output: true
console.log(isValidDate(date2)); // Output: false
console.log(isValidDate(date3)); // Output: false
console.log(isValidDate(date4)); // Output: false
note

By using this check, you can avoid the error.

let myDate = Date.now(); // An incorrect value

if (isValidDate(myDate)) {
console.log(myDate.getTime());
} else {
console.error('The provided value is not a valid Date object.');
}

Practical Example: A Robust Timestamp Conversion Function

This function is designed to take any input, safely attempt to convert it to a Date object, and return its timestamp without crashing.

function getTimestamp(input) {
let date = new Date(input);

// Use our validation logic here!
if (isNaN(date.getTime())) {
// Return null or throw an error for invalid inputs
return null;
}

return date.getTime();
}

// Example Usage:
console.log(getTimestamp('2023-10-27')); // Output: (timestamp number)
console.log(getTimestamp(new Date())); // Output: (timestamp number)
console.log(getTimestamp('not a real date')); // Output: null (No error thrown!)
console.log(getTimestamp(Date.now())); // Output: (timestamp number)

Conclusion

The TypeError: date.getTime is not a function is a clear signal that you are trying to call a Date-specific method on a value of the wrong type.

To solve it:

  • Understand the critical difference: new Date() creates a Date object, while Date.now() returns a Number.
  • Always validate your variable before calling .getTime() on it, especially if it comes from an external source or an unfamiliar function.
  • The most robust check is to ensure the value is both an instanceof Date and that its time value is not NaN.