ruby 将日期转化为时间
Ruby数据和时间类 (Ruby Data and Time Classes)
In any program written in any language, at any point of time you may need to fetch the date and time of the system at that instant. Ruby facilitates you with three classes related to Date and Time namely:
在用任何语言编写的任何程序中,您可能需要在任何时间获取该时刻的系统日期和时间。 Ruby通过与日期和时间相关的三个类为您提供便利:
Date
日期
DateTime
约会时间
Time
时间
If you are working with dates, you are required to understand some of the terms which are related to date such as:
如果使用日期,则需要了解一些与日期有关的术语,例如:
Calendar date: You must be familiar with the term. It is a specific day of a calendar month of a distinct year.
日历日期 :您必须熟悉该术语。 它是不同年份的日历月的特定日期。
Ordinal date: When you identify a specific date of a year with the help of an ordinary year. It is an ordinal date.
原始日期 :当您借助普通年份确定特定的日期时。 这是一个序数日期。
Week date: It is the combination of calendar week and number of days. The week that includes the first Thursday of the distinct year is the first calendar week of that year.
周日期 :是日历周和天数的组合。 包括不同年份的第一个星期四的一周是该年的第一个日历周。
Julian day number: It is a kind of progression of date since noon on January 1, 4713 BCE.
朱利安天数 :是公元前4713年1月1日中午以来日期的一种演变。
Now, let us talk about all those three classes related to date and Time which we have mentioned above in a detailed manner.
现在,让我们详细讨论上面提到的与日期和时间相关的所有这三个类。
1)日期 (1) Date)
Date class objects are mutable means that they cannot modify themselves at any point of time during the execution process. You will need to include date class in Ruby code before implementing date objects. You can create a date object with ::new, ::parse, ::today, ::jd, ::strptime.
日期类对象是可变的,意味着它们无法在执行过程中的任何时间修改自己。 在实现日期对象之前,您将需要在Ruby代码中包括日期类 。 您可以使用:: new , :: parse , :: today , :: jd和:: strptime创建日期对象。
Now let us see, how date objects are created with the help of an example.
现在让我们看看如何借助示例创建日期对象 。
=begin
Ruby program to show the implementation of
date class.
=end
require 'date'
puts Date.new(2020,4,12)
puts Date.jd(2451877)
puts Date.ordinal(2020,3)
puts Date.commercial(2020,12,6)
puts Date.parse('2020-02-03')
puts Date.strptime('03-02-2020', '%m-%d-%Y')
Output:
输出:
2020-04-12
2000-11-28
2020-01-03
2020-03-21
2020-02-03
2020-03-02
Here,
这里,
new: new method will create a new object depending upon the parameters passed.
new :new方法将根据传递的参数创建一个新对象。
jd: It is provided with the local time and returns the Julian day number.
jd :提供当地时间,并返回儒略日。
ordinal: It creates a date object depending upon the parameter passed. The parameter may be negative or positive but can never be zero.
ordinal :根据传递的参数创建日期对象。 参数可以是负数或正数,但永远不能为零。
commercial: It creates a date object depending upon the provided week date. The parameter can be negative or positive but can never be zero.
commercial :根据提供的星期日期创建日期对象。 参数可以是负数或正数,但永远不能为零。
parse: It creates a date object and parses the representation of date and time.
parse :它创建一个日期对象并解析日期和时间的表示形式。
strptime: It returns a hash of parsed elements after parsing the provided representation of date of time along with the specified template.
strptime :在解析提供的时间日期表示形式和指定模板之后,它返回解析元素的哈希值。
2)日期时间 (2) DateTime)
This class is a subclass of Date class. You can create an object of DateTime class with the help of DateTime.new, DateTime.ordinal, DateTime.parse, DateTime.jd, DateTime.commercial, DateTime.now, and so on. Let us see its example:
此类是Date类的子类。 您可以在DateTime.new , DateTime.ordinal , DateTime.parse , DateTime.jd , DateTime.commercial , DateTime.now等帮助下创建DateTime类的对象。 让我们看一下它的例子:
=begin
Ruby program to show the implementation of
DateTime class.
=end
require 'date'
d = DateTime.parse('12th Oct 2020 13:37:05+05:40')
puts d.hour
puts d.min
puts d.sec
puts d.offset
Output
输出量
13
37
5
17/72
In the above code, we are storing date in an object after parsing it. Then we are calling its multiple methods like an hour, min, sec and offset.
在上面的代码中,我们将日期解析后存储在对象中。 然后我们调用它的多个方法,例如小时 , 分钟 , 秒和偏移量 。
3)时间 (3) Time)
Time class is an abstraction of Date and DateTime class. You can create its objects with the help of ::new which will use the current system's time. You can pass year, month, day, hour, min, sec in the parameter list. An example is given below:
时间类是Date和DateTime类的抽象。 您可以在:: new的帮助下创建其对象,这将使用当前系统的时间。 您可以在参数列表中传递year , month , day , hour , min , sec 。 下面是一个示例:
=begin
Ruby program to show the implementation of
Time class.
=end
time1 = Time.new
puts "Printing Current Time: "+ time1.inspect
puts time1.year
puts time1.month
puts time1.day
puts time1.hour
puts time1.min
puts time1.sec
puts time1.zone
Output
输出量
Printing Current Time: 2019-09-25 06:26:58 +0530
2019
9
25
6
26
58
India Standard Time
翻译自: https://www.includehelp.com/ruby/date-and-time-classes.aspx
ruby 将日期转化为时间