Bug/JavaScript

[JavaScript] date.getTime is not a function Error

brightlightkim 2022. 4. 15. 02:23

Solve - date.getTime is not a function Error #

The "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.

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, which returns an integer and tried to call the Date.getTime() method on it, which caused the error.

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...

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 NaN (not a number) value back.

index.js
const t1 = new Date('invalid').getTime();
console.log(t1); // 👉️ NaN

from https://bobbyhadz.com/blog/javascript-typeerror-date-gettime-is-not-a-function#:~:text=getTime%20is%20not%20a%20function%22%20error%20occurs%20when%20the%20getTime,method%20on%20valid%20date%20objects.

 

Solve - date.getTime is not a function Error in JavaScript | bobbyhadz

The "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 vali

bobbyhadz.com

 

'Bug > JavaScript' 카테고리의 다른 글

Window Selection Object  (0) 2023.04.19
[JavaScript] 7 Native Errors in JavaScript  (0) 2022.04.29
[JavaScript] Default Parameters  (0) 2022.04.16