gpt4 book ai didi

javascript - 使用天/周和标题创建带有时刻的自定义日历

转载 作者:太空狗 更新时间:2023-10-29 14:40:57 26 4
gpt4 key购买 nike

我正在使用 moment 和 React 创建一个计划组件。我创建了一个自定义日历。如何像普通日历一样将星期几固定在顶部?因此,如果我在 9 月传递日期对象,我想用每个日期上方正确的星期几来呈现所有日期。我在这里创建了一个 fiddle 。

https://jsfiddle.net/4dbwLg6b/

var dates = ['Thu Sep 01 2016 00:00:00 GMT+0800 (CST)', 'Fri Sep 02 2016 00:00:00 GMT+0800 (CST)', 'Sat Sep 03 2016 00:00:00 GMT+0800 (CST)', 'Sun Sep 04 2016 00:00:00 GMT+0800 (CST)', 'Mon Sep 05 2016 00:00:00 GMT+0800 (CST)', 'Tue Sep 06 2016 00:00:00 GMT+0800 (CST)', 'Wed Sep 07 2016 00:00:00 GMT+0800 (CST)', 'Thu Sep 08 2016 00:00:00 GMT+0800 (CST)', 'Fri Sep 09 2016 00:00:00 GMT+0800 (CST)', 'Sat Sep 10 2016 00:00:00 GMT+0800 (CST)', 'Sun Sep 11 2016 00:00:00 GMT+0800 (CST)', 'Mon Sep 12 2016 00:00:00 GMT+0800 (CST)', 'Tue Sep 13 2016 00:00:00 GMT+0800 (CST)',' Wed Sep 14 2016 00:00:00 GMT+0800 (CST)',' Thu Sep 15 2016 00:00:00 GMT+0800 (CST)', 'Fri Sep 16 2016 00:00:00 GMT+0800 (CST)', 'Sat Sep 17 2016 00:00:00 GMT+0800 (CST)', 'Sun Sep 18 2016 00:00:00 GMT+0800 (CST)', 'Mon Sep 19 2016 00:00:00 GMT+0800 (CST)',' Tue Sep 20 2016 00:00:00 GMT+0800 (CST)', 'Wed Sep 21 2016 00:00:00 GMT+0800 (CST)', 'Thu Sep 22 2016 00:00:00 GMT+0800 (CST)', 'Fri Sep 23 2016 00:00:00 GMT+0800 (CST)', 'Sat Sep 24 2016 00:00:00 GMT+0800 (CST)', 'Sun Sep 25 2016 00:00:00 GMT+0800 (CST)', 'Mon Sep 26 2016 00:00:00 GMT+0800 (CST)', 'Tue Sep 27 2016 00:00:00 GMT+0800 (CST)', 'Wed Sep 28 2016 00:00:00 GMT+0800 (CST)', 'Thu Sep 29 2016 00:00:00 GMT+0800 (CST)', 'Fri Sep 30 2016 00:00:00 GMT+0800 (CST)']
var Hello = React.createClass({
render: function() {


return <div className="container">
{dates.map(function(day){
return(
<div className="calendarDay">
{moment(day).format('D').toString()}
</div>
)
})}
</div>;
}
});

ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);

这就是我的 enter image description here

这就是我想要的,对应于日期编号的星期几。星期一,星期二,星期三..... enter image description here

最佳答案

基于以上所有答案:

const calendar = [];
const today = moment();
const startDay = today.clone().startOf('month').startOf('week');
const endDay = today.clone().endOf('month').endOf('week');

let date = startDay.clone().subtract(1, 'day');

while (date.isBefore(endDay, 'day'))
calendar.push({
days: Array(7).fill(0).map(() => date.add(1, 'day').clone())
});

关于javascript - 使用天/周和标题创建带有时刻的自定义日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39786372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com