Forum: Ruby need only time

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bee1159661d43b0c5b3410d49e95fc51?d=identicon&s=25 Shanmu Gam (shanmu)
on 2008-12-10 11:01
hi all,
how to get time alone from this format
(1986, 1, 28, 17, 39, 18)
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2008-12-10 11:49
Shanmu Gam wrote:
> hi all,
> how to get time alone from this format
> (1986, 1, 28, 17, 39, 18)

Use Time.mktime

3cb4fdcf13aad6a7dcae83876b0e784e?d=identicon&s=25 Josef 'Jupp' Schugt (Guest)
on 2008-12-10 12:21
(Received via mailing list)
On Wed, 10 Dec 2008 10:54:29 +0100, Shanmu Gam
<> wrote:

> how to get time alone from this format
> (1986, 1, 28, 17, 39, 18)

Given the numbers I suppose that the format is as follows:

    datetime = [1986, 1, 28, 17, 39, 18] # [Y, m, d, H, M, S]; array of

where Y is the year, m is the month, d the day, H the hour, M the minute
and S the second (at least this is the order of arguments provided to
Time.utc and similar functions). I am not sure what you mean by "get
time". For a string representation of the time you could use:


which in the given example returns


The same could also be done without using any Time functions using

    "%02d:%02d:%0d" % datetime[-3..-1]

where %02d results in a zero-padded (hence the 0) two character (hence
the 2) representation of  the values. While in this case it is not
needed, it makes sure that that (0, 8, 15, 1, 2, 3) results in
"01:02:03" and not in "1:2:3".

In the case you start with

    datetime = Time.utc(1986, 1, 28, 17, 39, 18)

and want a numerical array of hour, minute and second you can use

    [datetime.hour, datetime.min, datetime.sec]

I hope I understood your question correctly.

Josef 'Jupp' Schugt
This topic is locked and can not be replied to.