ROR + Ruby Date 减少 15 分钟

发布于 2024-11-09 12:19:34 字数 206 浏览 0 评论 0原文

如果我有 @time = Time.now.strftime("%Y-%m-%d %H:%M:%S")

如何将此时间减少 15 分钟?

我已经尝试过这个 :: @reducetime = @time-15.mins,在控制台上工作正常,但在执行时出错。除此之外还有什么办法可以解决这个问题。

谢谢

If I have @time = Time.now.strftime("%Y-%m-%d %H:%M:%S"),

How can I reduce this time by 15 minutes ?

I already tried this one :: @reducetime = @time-15.minutes, works fine at console but give errors while execution. Other than this Is there any way to resolve this issue.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-11-16 12:19:34

您的问题是您在将时间视为时间之前将其格式化为字符串。这会更有意义:

@time       = Time.now
@reducetime = @time - 15.minutes

# And then later when you're reading to display @time...
formatted_time = @time.strftime("%Y-%m-%d %H:%M:%S")

在准备好显示数据之前,您不应该格式化数据。

如果您必须将 @time 作为格式化时间,那么您必须在计算 @reducetime 之前解析它:

@reducetime = (DateTime.strptime(@time, "%Y-%m-%d %H:%M:%S") - 15.minutes).to_time

Your problem is that you're formatting your time into a string before you're done treating it as a time. This would make more sense:

@time       = Time.now
@reducetime = @time - 15.minutes

# And then later when you're reading to display @time...
formatted_time = @time.strftime("%Y-%m-%d %H:%M:%S")

You shouldn't format your data until right before you're ready to display it.

If you must have @time as the formatted time then you're going to have to parse it before computing @reducetime:

@reducetime = (DateTime.strptime(@time, "%Y-%m-%d %H:%M:%S") - 15.minutes).to_time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文