Formatting date without leading zeros

T

Tom Reinhart

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
(e-mail address removed)
http://alltom.com/
 
V

Vance A Heron

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
 
D

David A. Black

Hi --

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?

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
 
N

nobu.nokada

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"
 
D

David A. Black

Hi --

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"

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"

:)


David
 
N

nobu.nokada

Hi,

At Sun, 15 May 2005 21:01:23 +0900,
David A. Black wrote in [ruby-talk:142696]:
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.

:-(
 
E

ES

Le 15/5/2005 said:
Hi,

At Sun, 15 May 2005 21:01:23 +0900,
David A. Black wrote in [ruby-talk:142696]:
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:

=3D> "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
 
D

David A. Black

--927295978-1453014158-1116171502=:22950
Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-1453014158-1116171502=:22950"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--927295978-1453014158-1116171502=:22950
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Le 15/5/2005 said:
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')
=3D> "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:

=3D> "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?


David

--=20
David A. Black
(e-mail address removed)
--927295978-1453014158-1116171502=:22950--
--927295978-1453014158-1116171502=:22950--
 
T

Tom Reinhart

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
(e-mail address removed)
http://alltom.com/
 
E

ES

Le 15/5/2005 said:
Hi --

Le 15/5/2005 said:
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')
=3D> "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:

=3D> "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
=20
David A. Black

E
 
T

tony summerfelt

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top