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


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


Time#strftime()是一個Time類方法,它根據給定格式字符串中的指令返回時間格式。

用法:Time.strftime()

參數:時間值


返回:時間格式根據給定格式字符串中的指令。

示例1:

# Ruby code for Time.strftime() method 
  
# loading library 
require 'time'
  
# declaring time  
a = Time.new(2019) 
  
# declaring time 
b = Time.new(2019, 10) 
  
# declaring time 
c = Time.new(2019, 12, 31) 
  
# Time  
puts "Time a : #{a}\n\n"
puts "Time b : #{b}\n\n"
puts "Time c : #{c}\n\n\n\n"
  
  
# strftime form  
puts "Time a strftime form : #{a.strftime("at %I:%M %p")}\n\n"
puts "Time b strftime form : #{b.strftime("Is published on %m/%d/%Y")}\n\n"
puts "Time c strftime form : #{c.strftime("at %I:%M %p")}\n\n"

輸出:

Time a : 2019-01-01 00:00:00 +0000

Time b : 2019-10-01 00:00:00 +0000

Time c : 2019-12-31 00:00:00 +0000



Time a strftime form : at 12:00 AM

Time b strftime form : Is published on 10/01/2019

Time c strftime form : at 12:00 AM

示例2:

# Ruby code for Time.strftime() method 
  
# loading library 
require 'time'
  
# loading library 
require 'time'
  
# declaring time  
a = Time.now 
  
# declaring time 
b = Time.new(1000, 10, 10) 
  
# declaring time 
c = Time.new(2020, 12) 
  
# Time  
puts "Time a : #{a}\n\n"
puts "Time b : #{b}\n\n"
puts "Time c : #{c}\n\n\n\n"
  
  
# strftime form  
puts "Time a strftime form : #{a.strftime("at %I:%M %p")}\n\n"
puts "Time b strftime form : #{b.strftime("Is published on %m/%d/%Y")}\n\n"
puts "Time c strftime form : #{c.strftime("at %I:%M %p")}\n\n"

輸出:

Time a : 2019-08-27 12:04:41 +0000

Time b : 1000-10-10 00:00:00 +0000

Time c : 2020-12-01 00:00:00 +0000



Time a strftime form : at 12:04 PM

Time b strftime form : Is published on 10/10/1000

Time c strftime form : at 12:00 AM



相關用法


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