ruby 将日期转化为时间_Ruby中的日期和时间类
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. =endrequire '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-02Here,
這里,
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. =endrequire 'date' d = DateTime.parse('12th Oct 2020 13:37:05+05:40') puts d.hour puts d.min puts d.sec puts d.offsetOutput
輸出量
13 37 5 17/72In 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. =endtime1 = Time.newputs "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.zoneOutput
輸出量
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 將日期轉化為時間
總結
以上是生活随笔為你收集整理的ruby 将日期转化为时间_Ruby中的日期和时间类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++局部对象是什么_程序员每日一题-G
- 下一篇: 给定数组A []和数字X,请检查A []