# More elegant way to condense a date/time string?

**URL:** <https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968>\
**Category:** Ruby\
**Created:** [October 22, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968 "2007-10-22T21:52:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/1 "2007-10-22T21:52:08Z")

</div>

Hey all,

I’m a relative ruby beginner, looking to create short-ish date/time  
strings. I came up with:

Time.now.xmlschema.delete(’:-’).chop.chop.chop.chop #yields something  
like 20071022T123910

Which works for me…but it seemed like I might be doing acrobatics to  
solve a problem that ruby can solve more elegantly. I’m mostly just  
looking for a unique alphanumeric string that’s not too long. (I  
chopped the timezone to make it a bit shorter.) The particular format  
I ended up with works well for me, but is not a requirement - another  
option I’d be happy with would be seconds (or smaller) since epoch,  
for example.

I ask because I enjoy learning to make my ruby more elegant.

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/2 "2007-10-22T21:55:40Z")

</div>

On Oct 22, 3:47 pm, jfry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> looking for a unique alphanumeric string that’s not too long. (I  
> chopped the timezone to make it a bit shorter.) The particular format  
> I ended up with works well for me, but is not a requirement - another  
> option I’d be happy with would be seconds (or smaller) since epoch,  
> for example.

Time.now.to\_i will give the number of seconds since the epoch.

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/3 "2007-10-22T22:07:56Z")

</div>

On 22/10/2007, jfry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> option I’d be happy with would be seconds (or smaller) since epoch,  
> for example.

You can use Time.now.to\_f.

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/4 "2007-10-22T22:26:39Z")

</div>

On Tue, Oct 23, 2007 at 04:50:07AM +0900, jfry wrote:

> looking for a unique alphanumeric string that’s not too long. (I  
> chopped the timezone to make it a bit shorter.) The particular format  
> I ended up with works well for me, but is not a requirement - another  
> option I’d be happy with would be seconds (or smaller) since epoch,  
> for example.
> 
> I ask because I enjoy learning to make my ruby more elegant.

While #to\_i will get you seconds since epoch, you should also be aware  
of  
the #strftime method, which works (nearly?) exactly like the standard C  
library call.

> Thanks,  
> Jeff  
> –Greg

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/5 "2007-10-22T22:58:15Z")

</div>

> for example.  
> Look up the ISO-8601 date/time formats, e.g. 1999-01-08 04:05:06

They’re standard, unambiguous, and have the handy property that  
alphabetical sorting is equivalent to date-order sorting.

- donald

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 22, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/6 "2007-10-22T23:17:03Z")

</div>

On Oct 22, 1:25 pm, Gregory S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> While #to\_i will get you seconds since epoch, you should also be aware of  
> the #strftime method, which works (nearly?) exactly like the standard C  
> library call.

Thanks all! I’m glad I asked.  
Jeff

---

<div class="post-metadata">

**Author:** ![jfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jfry](https://www.ruby-forum.com/u/jfry)\
**Post date:** [October 23, 2007, 11:40am UTC](https://www.ruby-forum.com/t/more-elegant-way-to-condense-a-date-time-string/117968/7 "2007-10-23T11:40:26Z")

</div>

2007/10/22, jfry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> looking for a unique alphanumeric string that’s not too long. (I  
> chopped the timezone to make it a bit shorter.) The particular format  
> I ended up with works well for me, but is not a requirement - another  
> option I’d be happy with would be seconds (or smaller) since epoch,  
> for example.
> 
> I ask because I enjoy learning to make my ruby more elegant.

The proper way to achieve your format would probably be this:

irb(main):015:0\> Time.now.strftime “%Y%m%dT%H%M%S”  
=\> “20071023T085747”

Kind regards

robert
