Introduir el temps actual com a objecte.
Script
Time.now.strftime("%Y-%m-d %H:%M:S") #=> "2016-07-27 08:45:42"
Simplificat:
Time.now.strftime("%F %X") #=> "2016-07-27 08:45:42"
Altra manera:
Time.now
Time.new # És equivalent si no usem paràmetres
Temps específic:
Time.new(2010, 3, 10) #10 March 2010 (Midnight)
Time.new(2015, 5, 3, 10, 14) #10:14 AM on 3 May 2015
Time.new(2050, "May", 3, 21, 8, 16, "+10:00") #09:08:16 PM on 3 May 2050
Exemple amb un arxiu anomenat temps.rb
:
#!/usr/bin/ruby -w
time1 = Time.new
puts "Current Time : " + time1.inspect
# Time.now is a synonym:
time2 = Time.now
puts "Current Time : " + time2.inspect
Output:
Current Time : 2019-06-01 10:20:26 +0200
Current Time : 2019-06-01 10:20:26 +0200
Més específic:
#!/usr/bin/ruby -w
time = Time.new
# Components of a Time
puts "Current Time : " + time.inspect
puts time.year # => Year of the date
puts time.month # => Month of the date (1 to 12)
puts time.day # => Day of the date (1 to 31 )
puts time.wday # => 0: Day of week: 0 is Sunday
puts time.yday # => 365: Day of year
puts time.hour # => 23: 24-hour clock
puts time.min # => 59
puts time.sec # => 59
puts time.usec # => 999999: microseconds
puts time.zone # => "UTC": timezone name
Output:
Current Time : 2019-06-01 10:27:22 +0200
2019
6
1
6
152
10
27
22
990647
CEST
Font: Tutorialspoint
Classe
class Horari
def temps
puts Time.now.strftime("%Y-%m-%d %H:%M:%S")
end
end
a = Horari.new()
a.temps
print
Font: riptutorial.com/ruby