# Need only time

**URL:** <https://www.ruby-forum.com/t/need-only-time/155297>\
**Category:** Ruby\
**Created:** [December 10, 2008, 11:01am UTC](https://www.ruby-forum.com/t/need-only-time/155297 "2008-12-10T11:01:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [December 10, 2008, 11:01am UTC](https://www.ruby-forum.com/t/need-only-time/155297/1 "2008-12-10T11:01:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [December 10, 2008, 11:49am UTC](https://www.ruby-forum.com/t/need-only-time/155297/2 "2008-12-10T11:49:12Z")

</div>

Shanmu G. wrote:

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

Use Time.mktime

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![shanmu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shanmu](https://www.ruby-forum.com/u/shanmu)\
**Post date:** [December 10, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/need-only-time/155297/3 "2008-12-10T12:21:39Z")

</div>

On Wed, 10 Dec 2008 10:54:29 +0100, Shanmu G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 

```

integers

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:

```
Time.utc(*datetime).strftime("%H:%M:%S")

```

which in the given example returns

```
"17:39:18"

```

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
