當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby DateTime strftime()用法及代碼示例


DateTime#strftime():strftime()是DateTime類方法,該方法將Formats date返回給定格式字符串中的指令。

用法:DateTime.strftime()

參數:日期時間值


返回:按照給定格式字符串中的指令進行操作。

示例1:

# Ruby code for DateTime.strftime() method 
  
# loading library 
require 'date'
  
# declaring DateTime value 
date_a = DateTime.new(2019, 8, 10, 4, 10, 9) 
  
# declaring DateTime value 
date_b = DateTime.new(2019, 8, 10.5) 
  
# declaring DateTime value 
date_c = DateTime.new(2019, 8, 10, 4, 10, 9, Rational(4, 24)) 
  
  
#  strftime method 
puts "DateTime strftime form : #{date_a.strftime("Printed on %m/%d/%Y")}\n\n"
  
puts "DateTime strftime form : #{date_b.strftime}\n\n"
  
puts "DateTime strftime form : #{date_c.strftime("at %I:%M%p")}\n\n"

輸出:

DateTime strftime form : Printed on 08/10/2019

DateTime strftime form : 2019-08-10T12:00:00+00:00

DateTime strftime form : at 04:10AM

示例2:

# Ruby code for DateTime.strftime() method 
  
# loading library 
require 'date'
  
# declaring DateTime value 
date_a = DateTime.new(2019, 8, 10, 5) 
  
# declaring DateTime value 
date_b = DateTime.parse('10 Aug 2018 04:10:06+04:30') 
  
# declaring DateTime value 
date_c = DateTime.new(2019, 8, 10, 4, 10, 9, '+03:00') 
  
  
#  strftime method 
puts "DateTime strftime form : #{date_a.strftime("Printed on %m/%d/%Y")}\n\n"
  
puts "DateTime strftime form : #{date_b.strftime}\n\n"
  
puts "DateTime strftime form : #{date_c.strftime("at %I:%M%p")}\n\n"

輸出:

DateTime strftime form : Printed on 08/10/2019

DateTime strftime form : 2018-08-10T04:10:06+04:30

DateTime strftime form : at 04:10AM



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | DateTime strftime() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。