[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Formatting date without leading zeros

Tom Reinhart

5/15/2005 8:06:00 AM

I've tried to do the best research I can into this before posting here,
but I have come up relatively short. I would like to format a DateTime
such that the day of the month and hours that are less than ten are not
prefixed with a leading zero (ex. Jan 4 9:30 and not Jan 04 09:30).

Is there any way to use DateTime#strftime to do this, or any
alternative short of piecing the bits together with calls like
thedate.month + " " + thedate.day?

Thank you.

Sincerely,

Tom Reinhart
tom@alltom.com
http://a...

11 Answers

Vance A Heron

5/15/2005 8:53:00 AM

0

Tom,
The best I could come up with is ...
heron $ irb
irb(main):001:0> t=Time.local(2005,'jan',4,9,05,0,0)
=> Tue Jan 04 09:05:00 PST 2005
irb(main):002:0> t.strftime('%b %d %Y %H:%M').gsub(/ 0(\d\D)/, ' \1')
=> "Jan 4 2005 9:05"
irb(main):003:0>

Hope this helps.

Vance
On Sun, 2005-05-15 at 17:10 +0900, Tom Reinhart wrote:
> I've tried to do the best research I can into this before posting here,
> but I have come up relatively short. I would like to format a DateTime
> such that the day of the month and hours that are less than ten are not
> prefixed with a leading zero (ex. Jan 4 9:30 and not Jan 04 09:30).
>
> Is there any way to use DateTime#strftime to do this, or any
> alternative short of piecing the bits together with calls like
> thedate.month + " " + thedate.day?
>
> Thank you.
>
> Sincerely,
>
> Tom Reinhart
> tom@alltom.com
> http://a...
>
>



dblack

5/15/2005 10:33:00 AM

0

nobu.nokada

5/15/2005 11:38:00 AM

0

Hi,

At Sun, 15 May 2005 17:53:05 +0900,
Vance A Heron wrote in [ruby-talk:142690]:
> irb(main):001:0> t=Time.local(2005,'jan',4,9,05,0,0)
> => Tue Jan 04 09:05:00 PST 2005
> irb(main):002:0> t.strftime('%b %d %Y %H:%M').gsub(/ 0(\d\D)/, ' \1')
> => "Jan 4 2005 9:05"
irb(main):003:0> t.strftime('%b %-2d %Y %-2H:%-2M')
=> "Jan 4 2005 9: 5"

--
Nobu Nakada


dblack

5/15/2005 12:01:00 PM

0

nobu.nokada

5/15/2005 2:47:00 PM

0

Hi,

At Sun, 15 May 2005 21:01:23 +0900,
David A. Black wrote in [ruby-talk:142696]:
> > irb(main):003:0> t.strftime('%b %-2d %Y %-2H:%-2M')
> > => "Jan 4 2005 9: 5"
>
> I hadn't thought to look there -- I'd tried something like:
>
> irb(main):002:0> Date.today.strftime('%b %-2d %Y %-2H:%-2M')
>
> which gave me:
>
> => "May %-2d 2005 %-2H:%-2M"

It looks like system dependent.

> :-)

:-(

--
Nobu Nakada


ES

5/15/2005 2:53:00 PM

0


Le 15/5/2005, "nobu.nokada@softhome.net" <nobu.nokada@softhome.net> a
écrit:
>Hi,
>
>At Sun, 15 May 2005 21:01:23 +0900,
>David A. Black wrote in [ruby-talk:142696]:
>> > irb(main):003:0> t.strftime('%b %-2d %Y %-2H:%-2M')
>> > => "Jan 4 2005 9: 5"
>>
>> I hadn't thought to look there -- I'd tried something like:
>>
>> irb(main):002:0> Date.today.strftime('%b %-2d %Y %-2H:%-2M')
>>
>> which gave me:
>>
>> => "May %-2d 2005 %-2H:%-2M"
>
>It looks like system dependent.

I think he was saying that Date will not do it while Time
will (with Matz-ue providing an explanation for this).

It is still, of course, system-dependent for Time.

>> :-)
>
>:-(
>
>Nobu Nakada

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


dblack

5/15/2005 3:39:00 PM

0

Tom Reinhart

5/15/2005 3:39:00 PM

0

> irb(main):001:0> t=Time.local(2005,'jan',4,9,05,0,0)
> => Tue Jan 04 09:05:00 PST 2005
> irb(main):002:0> t.strftime('%b %d %Y %H:%M').gsub(/ 0(\d\D)/, ' \1')
> => "Jan 4 2005 9:05"
> irb(main):003:0>
>
> Hope this helps.
>
> Vance

Okay, so it looks like I can use a regular expression for removing the
leading zero ...

> I think the %e and %l modifiers will give you the day of month and
the
> hour without 0's, but with spaces (i.e., space-padded). Looking at
> date/format.rb I don't see a way to suppress the padding.
>
> David

.... or I can use a regular expression for removing the repeated spaces.
(Thanks for the hints about using Time.strftime, but if it's really
system dependent, I think I'll go the regex route.) Thanks, all, very
much. :)

Sincerely,

Tom Reinhart
tom@alltom.com
http://a...

ES

5/15/2005 3:43:00 PM

0


Le 15/5/2005, "David A. Black" <dblack@wobblini.net> a écrit:
>Hi --
>
>On Sun, 15 May 2005, ES wrote:
>
>>
>> Le 15/5/2005, "nobu.nokada@softhome.net" <nobu.nokada@softhome.net> a
>> écrit:
>>> Hi,
>>>
>>> At Sun, 15 May 2005 21:01:23 +0900,
>>> David A. Black wrote in [ruby-talk:142696]:
>>>>> irb(main):003:0> t.strftime('%b %-2d %Y %-2H:%-2M')
>>>>> => "Jan 4 2005 9: 5"
>>>>
>>>> I hadn't thought to look there -- I'd tried something like:
>>>>
>>>> irb(main):002:0> Date.today.strftime('%b %-2d %Y %-2H:%-2M')
>>>>
>>>> which gave me:
>>>>
>>>> => "May %-2d 2005 %-2H:%-2M"
>>>
>>> It looks like system dependent.
>>
>> I think he was saying that Date will not do it while Time
>> will (with Matz-ue providing an explanation for this).
>
>Yes, except for the part about Matz providing an explanation. Or did
>I miss that?

Actually, it was a separate thread, I did not even realize it.
Essentially, Date is parsed with code by the Ruby team while
Time is parsed with strftime from the underlying platform.

See thread "Possible bug in Date/ParseDate"

>David
>
>David A. Black

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


tony summerfelt

5/15/2005 6:51:00 PM

0

> See thread "Possible bug in Date/ParseDate"

i think that was the thread i started.

i write a lot of log/time manipulation code (in all the languages i
work in) so i usually run into date/time bugs or inconsistencies...

--
http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org