簡體   English   中英

在MySQL中格式化時間數據類型

[英]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") }

要實現此目的,需要兩個步驟:

  1. Time.parse從數據庫返回的字符串轉換為Time的實例。
  2. 使用Time#strftimeTime實例格式化為所需的輸出格式。

要僅顯示時間的小時,分​​鍾和秒(不包含日期和時區信息),請使用'%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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM