admin管理员组文章数量:1291839
I keep getting this error:
TypeError: date.getHours is not a function
when trying to use .getHours
and I'm not sure why. This is the code I'm using:
import React from "react";
import "../Nav/Nav.css";
import Date from "./Date";
function NavBar() {
var date = new Date();
var hrs = date.getHours();
var greeting;
if (hrs < 12) greeting = "Good Morning";
else if (hrs >= 12 && hrs <= 17) greeting = "Good Afternoon";
else if (hrs >= 17 && hrs <= 24) greeting = "Good Evening";
return (
<div className="nav-container">
<p className="greeting">{greeting}</p>
<Date />
</div>
);
}
export default NavBar;
I keep getting this error:
TypeError: date.getHours is not a function
when trying to use .getHours
and I'm not sure why. This is the code I'm using:
import React from "react";
import "../Nav/Nav.css";
import Date from "./Date";
function NavBar() {
var date = new Date();
var hrs = date.getHours();
var greeting;
if (hrs < 12) greeting = "Good Morning";
else if (hrs >= 12 && hrs <= 17) greeting = "Good Afternoon";
else if (hrs >= 17 && hrs <= 24) greeting = "Good Evening";
return (
<div className="nav-container">
<p className="greeting">{greeting}</p>
<Date />
</div>
);
}
export default NavBar;
Share
Improve this question
edited Jan 1, 2020 at 14:15
Nicholas Tower
85.2k10 gold badges105 silver badges119 bronze badges
asked Jan 1, 2020 at 13:38
Leslie TLeslie T
631 gold badge1 silver badge4 bronze badges
10
- Hi Leslie, wele to SO, when I run this code it seems to work fine. Can you create a minimal reproducible example ? – Nick Parsons Commented Jan 1, 2020 at 13:41
- Maybe you are using an old browser? – Mikkel Commented Jan 1, 2020 at 13:59
- Hi Nick, thank you! I'm not really sure why because I was working a few days ago. – Leslie T Commented Jan 1, 2020 at 14:00
- Mikkel, I'm using chrome. I believe it's up to date but I'll double check – Leslie T Commented Jan 1, 2020 at 14:06
-
In your latest code you show that
Date
is not the built in Date object, but some file you're importing. Can you show us that file? Alternatively, if you mean to use normal dates, delete that import. – Nicholas Tower Commented Jan 1, 2020 at 14:13
2 Answers
Reset to default 2I just tried to recreate the issue. Copied the piece of code from your example and here is the result:
https://codepen.io/tural-ali/pen/vYEeaRK
var date = new Date();
var hrs = date.getHours();
alert(hrs);
It works totally fine.
That means it's something related to your imports:
import Date from "./Date";
is the line that causes the error. Try to create alias for this import as:
import Date as DateObject from "./Date";
you can not use Date as ponent
and Date as Date
object
please change
import Date from "./Date";
...
<Date />
to
import Date as ObjDate from "./Date";
...
<ObjDate />
本文标签: javascriptTypeError dategetHours is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: date.getHours is not a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741539053a2384210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论