javascript - 从现在到一天结束的所有时间
问题描述
所以我想把一天中所有剩余的时间分成一个数组,例如,如果实际时间是下午 3:00,我想有一个数组,例如 [4pm, 5pm, 6pm, 7pm, ... , 11pm]
我没有运气使用moment.js尝试了这样的事情
var now = moment().startOf('hour');
$('div').append(now + "<br>");
var count = 0;
while (now < moment().endOf('day')) {
count += 30;
now = now.add(count, 'minutes').format("hh:mm a");
$('div').append(now + "<br>");
}
我怎样才能达到我想要的?
解决方案
您可以创建一个包含所有小时数的列表并删除n
第一个条目,即n
当前 24 小时时间,如下所示:
var now = moment().startOf('hour');
var all_hours=['12pm', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12am', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
var remaining_hours=all_hours.slice(parseInt(now.format("H")), all_hours.length-1);
话虽如此,我认为您进行循环的原因是因为这是一个MVCE,而不是您在生产中真正要做的事情。因此,基于您的示例,以下内容应该有效
// Get current hour
var now = moment().startOf('hour');
// Get the 24 hour time
var this_hr_int24=parseInt(now.format("H"));
// The list to contain the remaining hours
var remaining_hours=[];
// Initialize loop variables
next_hr_int24=this_hr_int24;
next_hr=now;
// While the number in next_hr_int24 is less then 24
while (next_hr_int24 < parseInt(moment().endOf('day').format("H"))) {
// Increase by 60
var count = 60;
// Next hour of day
next_hr = next_hr.add(count, 'minutes')
// Get the 24 hr time for the next hour
next_hr_int24 = next_hr.format("H")
// Get the am/pm value for the list
next_hr_apm = next_hr.format("h a")
remaining_hours.push(next_hr_apm);
}
推荐阅读
- python - 使用带有表单数据的选择值提交表单
- python - 我在哪里可以找到 Redshift 中的列统计信息?
- linux - 作为根组成员无权查看文件
- python-3.x - 获取字典中的值以不更新。蟒蛇 3.6.9
- spring - 为什么我不能通过 Gradle 下载工件?
- amazon-web-services - Terraform 和 Elastic Beanstalk 版本
- angular - 从现有列表格式化新的 json
- visual-studio-code - VSCode 扩展:如何在链接命令的启用子句为 false 时禁用树项
- python - 手动关闭应用程序时,带有 Selenium 的 Python 脚本使 firefox.exe 运行
- node.js - '包'nodejs'未安装,因此未删除'Ubuntu 18.04