javascript エラー「Uncaught TypeError: xxx.getMinutes is not a function」の解決方法
- 作成日 2022.10.27
- javascript
- javascript
javascriptで、エラー「Uncaught TypeError: xxx.getMinutes is not a function」が発生した場合の原因と解決方法を記述してます。「Date.now()」のような有効な日付オブジェクト以外に「getMinutes」を使用した際に発生します。「chrome」や「firefox」や「safari」の各ブラウザのエラーメッセージの画像もキャプチャしてます。
環境
- OS windows11 pro 64bit
- Apache 2.4.43
- ブラウザ chrome 106.0.5249.119
エラー内容
以下の、「Date.now()」に対して日時の「分」を取得する「getMinutes」を実行したコードにて発生。
const date = Date.now();
const result = date.getMinutes();
エラーメッセージ
Uncaught TypeError: date.getMinutes is not a function
画像
firefox105の場合でも同じエラーが発生します。
Uncaught TypeError: date.getMinutes is not a function
画像
safari15.5では、以下のエラーとなります。
TypeError: date.getMinutes is not a function. (In 'date.getMinutes()', 'date.getMinutes' is undefined)
画像
原因
「Date.now()」は、有効な日付オブジェクトではないため
const date = Date.now();
console.log(date); // 1666748431393
解決方法
「new Date()」を使用する
const date = new Date();
const result = date.getMinutes();
console.log( result );
または、「new Date()」であるかを判定して使用します。
const date = Date.now();
const result = toString.call(date).slice(8, -1) === 'Date' ? date.getMinutes() : '有効な日付オブジェクトではありません';
console.log( result );
// 有効な日付オブジェクトではありません
-
前の記事
EXCEL 太字に変更するショートカットキー 2022.10.27
-
次の記事
javascript 二次元配列をオブジェクトに変換する処理で「Object.fromEntries」と「reduce」と「for文」とのパフォーマンスを計測する 2022.10.27
コメントを書く