# DateTime beauty

**URL:** <https://www.ruby-forum.com/t/datetime-beauty/47922>\
**Category:** Ruby\
**Created:** [November 15, 2005, 6:09am UTC](https://www.ruby-forum.com/t/datetime-beauty/47922 "2005-11-15T06:09:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhausman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c4cdca/32.png) [@hhausman](https://www.ruby-forum.com/u/hhausman)\
**Post date:** [November 15, 2005, 6:09am UTC](https://www.ruby-forum.com/t/datetime-beauty/47922/1 "2005-11-15T06:09:29Z")

</div>

I’ve got strings created by doing something on the order of  
DateTime.now.to\_s stored away.

When I’m reading them out, and showing them to the user I want to see  
something pretty like:

Sat Mar 10 at 12:34pm

Heres the code I came up with:

def timeFormat( in\_str )  
the\_date = DateTime.parse( in\_str )  
the\_date = the\_date.new\_offset( -7.0/24.0 )  
nice\_date = ‘’  
nice\_date += case the\_date.cwday  
when 1; "Mon "  
when 2; "Tue "  
when 3; "Wed "  
when 4; "Thu "  
when 5; "Fri "  
when 6; "Sat "  
when 7; "Sun "  
end  
nice\_date += case the\_date.month  
when 1; "Jan "  
when 2; "Feb "  
when 3; "Mar "  
when 4; "Apr "  
when 5; "May "  
when 6; "Jun "  
when 7; "Jul "  
when 8; "Aug "  
when 9; "Sep "  
when 10; "Oct "  
when 11; "Nov "  
when 12; "Dec "  
end  
nice\_date += the\_date.day.to\_s + ’ at ’  
the\_minute = the\_date.min  
if( the\_minute \< 10 )  
the\_minute = “0”+the\_minute.to\_s  
else  
the\_minute = the\_minute.to\_s  
end  
if( the\_date.hour \< 12 )  
nice\_date += the\_date.hour.to\_s + “:” + the\_minute + “am”  
elsif( the\_date.hour \> 12 )  
nice\_date += ( the\_date.hour-12 ).to\_s + “:” + the\_minute + “pm”  
else  
nice\_date += ( the\_date.hour ).to\_s + “:” + the\_minute + “pm”  
end  
nice\_date  
end

#Woah, ugly.  
#Anyone spare a moment to show me the right way to do something like  
this?

#Thanks in advance  
#-Harold

---

<div class="post-metadata">

**Author:** ![hhausman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c4cdca/32.png) [@hhausman](https://www.ruby-forum.com/u/hhausman)\
**Post date:** [November 15, 2005, 6:36am UTC](https://www.ruby-forum.com/t/datetime-beauty/47922/2 "2005-11-15T06:36:34Z")

</div>

It should just be a matter of using strftime:

value.strftime("%a %b %e at %I:%M%p")

- Jamis

---

<div class="post-metadata">

**Author:** ![hhausman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c4cdca/32.png) [@hhausman](https://www.ruby-forum.com/u/hhausman)\
**Post date:** [November 15, 2005, 6:45am UTC](https://www.ruby-forum.com/t/datetime-beauty/47922/3 "2005-11-15T06:45:36Z")

</div>

Wow, that works like a charm, thanks.

-Harold

---

<div class="post-metadata">

**Author:** ![hhausman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c4cdca/32.png) [@hhausman](https://www.ruby-forum.com/u/hhausman)\
**Post date:** [November 15, 2005, 7:18am UTC](https://www.ruby-forum.com/t/datetime-beauty/47922/4 "2005-11-15T07:18:41Z")

</div>

Harold H. wrote:

> On 11/14/05, Jamis B. wrote:
> 
> > It should just be a matter of using strftime:
> > 
> > value.strftime("%a %b %e at %I:%M%p")
> 
> Wow, that works like a charm, thanks.

Just ignore this, then 🙂

require ‘date’

def time\_fmt2(str)  
DateTime.parse(str).  
new\_offset(-7/24.0).  
strftime(’%a %b %d at %I:%M%p’).  
sub(/[AP]M\z/) {|xm| xm.downcase}  
end

p time\_fmt2(‘2005/11/15 05:59’) # “Mon Nov 14 at 10:59pm”

daz
