javascript - 使用倒计时日期保存当前进度 - 进度条引导程序 4
问题描述
我想使用进度条引导程序 4 创建动态倒计时日期进度,
我们可以设置百分比以使用宽度样式在标签上显示进度,但输入将使用日期格式(它将在管理员中插入)。所以这就是我所做的:
当在管理员上插入日期时,我接受它,然后在 JS 上进行计算以获取秒数,即
“(在管理员上插入的截止日期)-(现在的当前日期时间)”=我将获得动态从这里开始的剩余时间的秒数var countTime = #{(properties.countdown_date.to_datetime.strftime('%a, %d %b %Y %H:%M:%S').to_time - Time.now.to_datetime.strftime('%a, %d %b %Y %H:%M:%S').to_time).to_i}
properties.countdown_date
= 是调用在admin上插入的截止日期的方法,格式示例为:DD-MM-YY H:M:S (31-03-2021 14:15:00)放置
countTime
成宽度样式<div class="progress" style="height: 1.3rem"> <div aria-valuemax="100" aria-valuemin="0" aria-valuenow="25" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar"> </div> </div> $(".progress-bar").css("width", countTime+"%");
但我意识到百分比仍然是秒,所以我需要将它再次转换为 100% 的百分比除以时间的剩余秒数。
知道怎么做吗?谢谢
解决方案
假设 countdown_date 是期限值,deadline_created_at_time 是创建期限时的时间戳,在ActiveSupport::TimeWithZone
然后
total_time_in_sec = countdown_date.to_i - deadline_created_at_time.to_i
time_past = Time.now.to_i - deadline_created_at_time.to_i
percentage = time_past/total_time_in_sec.to_f*100
推荐阅读
- json - 如何创建具有日期索引的模型?
- java - 如何使用 RecyclerView 从 Firebase 获取密钥
- python - 将二维数组转换为图像
- javascript - 如何使用嵌套组件对组件进行快照测试?
- ios - Swift - 根据日期数组对不同的数组进行排序
- php - 如何在 WordPress 中为已注销的用户添加重定向异常
- python - 以 JSON 格式发送 OpenCV 图像
- git - 如何将 GitHub QGumboParser 库添加到 macOS 上的 Qt 项目?
- javascript - 来自屏蔽输入文本的时刻 js 无效日期
- c++ - 无法手动调用 std::string 的析构函数