本文簡要介紹ruby語言中 Time.new
的用法。
用法
new(year = (now = true), mon = nil, mday = nil, hour = nil, min = nil, sec = nil, zone = nil, in: nil)
根據給定的參數返回一個新的 Time 對象。
沒有位置參數,返回 Time.now
的值:
Time.new # => 2021-04-24 17:27:46.0512465 -0500
否則,根據給定的參數返回一個新的 Time 對象:
Time.new(2000) # => 2000-01-01 00:00:00 -0600
Time.new(2000, 12, 31, 23, 59, 59.5) # => 2000-12-31 23:59:59.5 -0600
Time.new(2000, 12, 31, 23, 59, 59.5, '+09:00') # => 2000-12-31 23:59:59.5 +0900
參數:
-
year
:整數年。 -
month
:月份值,可能是:-
1..12
範圍內的整數月份。 -
與正則表達式
/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/i
匹配的 3 字符字符串。
-
-
day
:1..31
範圍內的整數天(某些月份小於 31)。 -
hour
:0..23
範圍內的整數小時。 -
min
:0..59
範圍內的整數分鍾。 -
zone
:時區,可能是:-
與 UTC 的字符串偏移量。
-
與 UTC 的單個字母偏移量,範圍為
'A'..'Z'
、'J'
(所謂的軍事時區),不包括在內。 -
整數秒數。
-
時區對象;有關詳細信息,請參閱時區參數。
-
-
in: zone
:時區zone
,可能如上。
相關用法
- Ruby Time.now用法及代碼示例
- Ruby Time.nsec用法及代碼示例
- Ruby Time.gmtime用法及代碼示例
- Ruby Time.at用法及代碼示例
- Ruby Time.utc_offset用法及代碼示例
- Ruby Time.isdst用法及代碼示例
- Ruby Time.time + numeric用法及代碼示例
- Ruby Time.wednesday?用法及代碼示例
- Ruby Time.localtime用法及代碼示例
- Ruby Time.yday用法及代碼示例
- Ruby Time.time <=>用法及代碼示例
- Ruby Time.month用法及代碼示例
- Ruby Time.utc?用法及代碼示例
- Ruby Time.monday?用法及代碼示例
- Ruby Time.getutc用法及代碼示例
- Ruby Time.round用法及代碼示例
- Ruby Time.dst?用法及代碼示例
- Ruby Time.tv_nsec用法及代碼示例
- Ruby Time.xmlschema用法及代碼示例
- Ruby Time.gmt?用法及代碼示例
- Ruby Time.to_r用法及代碼示例
- Ruby Time.gmt_offset用法及代碼示例
- Ruby Time.ceil用法及代碼示例
- Ruby Time.parse用法及代碼示例
- Ruby Time.thursday?用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Time.new。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。