我正在嘗試使用時間戳顯示時間。一個例子如下:將時間戳轉換爲時間
t = Time.at(1500999892331)
#=> 49534-10-18 04:02:11 +0530
t.to_date
#=> Thu, 18 Oct 49534
我得到一個錯誤的輸出。請幫我解決這個問題。
我正在嘗試使用時間戳顯示時間。一個例子如下:將時間戳轉換爲時間
t = Time.at(1500999892331)
#=> 49534-10-18 04:02:11 +0530
t.to_date
#=> Thu, 18 Oct 49534
我得到一個錯誤的輸出。請幫我解決這個問題。
Ruby的Time.at
預計秒(含分數)。每定義
創建以來大紀元給定數的
seconds_with_frac
,或seconds
和microseconds_with_frac
由時間給出的值,一個新的Time對象。
您提供的值爲毫秒。將其更改爲
Time.at(1500999892331/1000)
=> 2017-07-25 21:54:52 +0530
t.to_date
=> Tue, 25 Jul 2017
你可以這樣做
(Time.at(1500999892331/1000)+0530).strftime("%I:%M%p")
#=> "10:00PM"
或
(Time.at(1500999892331/1000)+0530).strftime("%Y-%b-%d %I:%M%p")
#=> "2017-Jul-25 10:00PM"
以下是一些日期和時間格式,你可以在strftime
法規定:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%C - year/100 (round down. 20 in 2009)
%y - year % 100 (00..99)
%m - Month of the year, zero-padded (01..12)
%_m blank-padded (1..12)
%-m no-padded (1..12)
%B - The full month name (``January'')
%^B uppercased (``JANUARY'')
%b - The abbreviated month name (``Jan'')
%^b uppercased (``JAN'')
%h - Equivalent to %b
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
%e - Day of the month, blank-padded (1..31)
%j - Day of the year (001..366)
Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded (0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded (1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')
%M - Minute of the hour (00..59)
%S - Second of the minute (00..59)
%L - Millisecond of the second (000..999)
%N - Fractional seconds digits, default is 9 digits (nanosecond)
%3N millisecond (3 digits)
%6N microsecond (6 digits)
%9N nanosecond (9 digits)
%12N picosecond (12 digits)
我怎樣才能在這裏得到日期? –
@ShruthiR回答更新請檢查 –
哪一部分是錯的,一個nd什麼是「正確的」輸出? – sawa