[英]Format Datatype for Time in MySQL
result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'")
equipments = result.map { |print_this| [print_this['sourcetime'], print_this['description']] }
p equipments
MySQL工作台中sourcetime
的數據類型設置為TIME
我得到的結果是2000-01-01 12:00:04 +0100
我只想獲取時間12:00:04
並刪除日期和+0100
您可以嘗試strftime(“%I:%M%p”)
equipments = result.map { |print_this| print_this.sourcetime.strftime("%I:%M%p") }
要實現此目的,需要兩個步驟:
Time.parse
從數據庫返回的字符串轉換為Time
的實例。 Time#strftime
將Time
實例格式化為所需的輸出格式。 要僅顯示時間的小時,分鍾和秒(不包含日期和時區信息),請使用'%H:%M:%S'
指令(有關更多示例,請參閱文檔 )。
require 'time' # `Time.parse` is not Ruby Core but from the Standard Library
equipments = result.map do |record|
[
Time.parse(record['sourcetime']).strftime('%H:%M:%S'),
record['description']
]
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.