给定一个时间点,希望得到其他时间点
问题:给定一个时间点,希望得到其他时间点
参考答案:
Ruby 的所有时间对象都可像数字一样用在值域中。Date和DateTime对象按天递增,而Time对象按秒递增:
require ‘date’
(Date.new(1776, 7, 2)..Date.new(1776, 7, 4)).each { |x| puts x }
# 1776-07-02
# 1776-07-03
# 1776-07-04
span = DateTime.new(1776, 7, 2, 1, 30, 15)..DateTime.new(1776, 7, 4, 7, 0, 0) span.each { |x| puts x }
# 1776-07-02T01:30:15Z
# 1776-07-03T01:30:15Z
# 1776-07-04T01:30:15Z
(Time.at(100)..Time.at(102)).each { |x| puts x }
# Wed Dec 31 19:01:40 EST 1969
# Wed Dec 31 19:01:41 EST 1969
# Wed Dec 31 19:01:42 EST 1969
Ruby 的Date类定义了step和upto两种方便的由数字使用的迭代器:
the_first = Date.new(2004, 1, 1)
the_fifth = Date.new(2004, 1, 5)
the_first.upto(the_fifth) { |x| puts x }
# 2004-01-01
# 2004-01-02
# 2004-01-03
# 2004-01-04
# 2004-01-05
参考答案:
Ruby 的所有时间对象都可像数字一样用在值域中。Date和DateTime对象按天递增,而Time对象按秒递增:
require ‘date’
(Date.new(1776, 7, 2)..Date.new(1776, 7, 4)).each { |x| puts x }
# 1776-07-02
# 1776-07-03
# 1776-07-04
span = DateTime.new(1776, 7, 2, 1, 30, 15)..DateTime.new(1776, 7, 4, 7, 0, 0) span.each { |x| puts x }
# 1776-07-02T01:30:15Z
# 1776-07-03T01:30:15Z
# 1776-07-04T01:30:15Z
(Time.at(100)..Time.at(102)).each { |x| puts x }
# Wed Dec 31 19:01:40 EST 1969
# Wed Dec 31 19:01:41 EST 1969
# Wed Dec 31 19:01:42 EST 1969
Ruby 的Date类定义了step和upto两种方便的由数字使用的迭代器:
the_first = Date.new(2004, 1, 1)
the_fifth = Date.new(2004, 1, 5)
the_first.upto(the_fifth) { |x| puts x }
# 2004-01-01
# 2004-01-02
# 2004-01-03
# 2004-01-04
# 2004-01-05
【给定一个时间点,希望得到其他时间点】相关文章
2. 请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值
5. 如何制定一份有效的计划书
6. JSP和Servlet有哪些相同点和不同点,他们之间的联系是什么?
7. Spring如何配置一个bean来从JNDI得到DataSource
9. 如何将一个描述日期或日期/时间的字符串转换为一个Date对象
10. 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数
本文来源:https://www.mianshiwenti.com/a13480.html
进入下载页面