Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

18.2. Date

使用strftime獲得具體的年份,月份或日期等等,常用:

		
CODE	OUTPUT	DESCRIPTION
t.strftime("%H")	=> "22"	# Gives Hour of the time in 24 hour clock format
t.strftime("%I")	=> "10"	# Gives Hour of the time in 12 hour clock format
t.strftime("%M")	=> "49"	# Gives Minutes of the time
t.strftime("%S")	=> "27"	# Gives Seconds of the time
t.strftime("%Y")	=> "2013"	# Gives Year of the time
t.strftime("%m")	=> "09"	# Gives month of the time
t.strftime("%d")	=> "12"	# Gives day of month of the time
t.strftime("%w")	=> "4"	# Gives day of week of the time
t.strftime("%a")	=> "Thu"	# Gives name of week day in short form of the
t.strftime("%A")	=> "Thursday"	# Gives week day in full form of the time
t.strftime("%b")	=> "Sep"	# Gives month in short form of the time
t.strftime("%B")	=> "September"	# Gives month in full form of the time
t.strftime("%y")	=> "13"	# Gives year without century of the time
t.strftime("%Y")	=> "2013"	# Gives year without century  of the time
t.strftime("%Z")	=> "IST"	# Gives Time Zone of the time
t.strftime("%p")	=> "PM"	# Gives AM / PM of the time		
		
		
		
# 2012-03-06 15:28:08
puts Time.now.strftime("%Y-%m-%d %H:%M:%S")

# 03/06/12 03:39 PM
puts Time.now.strftime("%x %I:%M %p")