javascript - 如何在momentjs中将开始时间设置为特定时间
问题描述
我正在尝试将一天的开始时间设置为特定时间。目前,在momentjs中,我可以像这样获得startOf day
let now = moment()
console.log('now', now.toString())
console.log('start Day', now.startOf('day').toString()) // Thu Oct 07 2021 00:00:00 GMT+0530
console.log('end day', now.endOf('day').toString()) //Thu Oct 07 2021 23:59:59 GMT+0530
有什么办法可以让我设置我的一天从特定时间开始,就像我想开始我的一天一样
2021 年 10 月 7 日星期四 08:00:00 GMT+0530
并结束
2021 年 10 月 7 日星期四 07:59:59 GMT+0530
解决方案
您可能应该编写自己的函数来实现这一点。
function customStartOf(momentObj) {
return momentObj.clone().startOf('day').hours(8);
}
function customEndOf(momentObj) {
// I assume that end of the day is bigger than start of the day
return momentObj.clone().endOf('day').add(1, 'days').hours(7);
}
let now = moment();
console.log('now', now.toString()) ;
console.log('start Day', now.startOf('day').toString());
console.log('end day', now.endOf('day').toString());
console.log('custom start Day', customStartOf(now).toString());
console.log('custom end day', customEndOf(now).toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
推荐阅读
- youtube - 如何从 Youtube 频道获得确切的订阅者数量?
- django - Django 反向嵌套过滤
- javascript - 节点项目中的 Git、Windows 10 和 *.js 文件存在问题
- c - 从文件恢复后出现损坏的 jpeg 图像
- flutter - 在 Flutter 中打开新屏幕后的上一屏 Bloc 调用
- python - 如何获取熊猫中每对唯一列的列值计数?
- angular - 选择元素的双向绑定无法正常工作 | 角 11
- matlab - 将矩阵中的垂直向量的值分配给 MATLAB r2019b 中的变量
- twitter-bootstrap - bootstrap 3.4.1 和 4.0.0 中元素的对齐外观和感觉不同
- javascript - 从 WebDataRocks “时间” JSON 类型中删除 :ss 部分