Ruby - Date Time Time creation

Introduction

The Time class also allows you to create Time objects based on arbitrary dates:

Time.local(year, month, day, hour, min, sec, msec) 

The preceding code creates a Time object based on the current (local) time zone.

All arguments from month onward are optional and take default values of 1 or 0.

You can specify months numerically (between 1 and 12), or as three-letter abbreviations of their English names.

The following code creates a Time object based on GMT/UTC. Argument requirements are the same as for Time.local.

Time.gm(year, month, day, hour, min, sec, msec) 

The following code is identical to Time.gm, although some might prefer this method's name.

Time.utc(year, month, day, hour, min, sec, msec) 

You can convert Time objects to an integer representing the number of seconds since the UNIX time epoch:

Demo

Time.gm(2007, 05).to_i

You can convert epoch times back into Time objects.

Demo

epoch_time = Time.gm(2007, 5).to_i 
t = Time.at(epoch_time) 
puts t.year, t.month, t.day

Result

Related Topic