[英]Javascript Date() return undefined
I am using Javasript's Date() method to extract current date simply.我正在使用 Javasript 的 Date() 方法来简单地提取当前日期。 It was working fine with my react native project but stopped suddenly.我的 react native 项目运行良好,但突然停止了。
let TodayDate = new Date(); console.log(TodayDate);
This prints me undefined
.这会打印我undefined
。 I tried testing with browser's console and it works fine but does not give any output in my files.我尝试使用浏览器的控制台进行测试,它工作正常,但在我的文件中没有提供任何输出。
My snippet I am trying:我正在尝试的片段:
getDateToday = () => { let dateToday = new Date(); console.log(dateToday); }; function componentDidMount() { this.getDateToday(); } componentDidMount();
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Kindly help me resolve the issue.请帮助我解决问题。
Change:改变:
new Date()
To:到:
new Date().toLocaleString();
Your code may look like:您的代码可能如下所示:
getDateToday = () => {
let dateToday = new Date().toLocaleString();
console.log(dateToday)
}
componentDidMount(){
this.getDateToday();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.