需要获取月份名称(即四月),而不是与 React 应用程序兼容的数字。
我尝试使用一些 JavaScript 片段,但没有得到我需要的结果。
当前使用 {(new Date().getMonth()+1)}
提取月份数字。请注意,这是在组件外部呈现。
最佳答案
最简单的形式是使用数组:
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let monthIndex = (new Date().getMonth());
let monthName = monthNames[monthIndex];
// render jsx
return (<div>{monthName}<div>)
如果您需要对日期执行更多操作(比较、加法、减法等),您可能需要使用库,例如moment.js .
关于javascript - react : Get month name instead of number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731490/