Forum: Ruby date formatting bug?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d042b437b937deb92d0c51515785a19?d=identicon&s=25 Mark Volkmann (mvolkmann)
on 2009-04-10 17:03
Why does the year get changed in this code?

require 'date'
s = '2998-12-31'
date = Date.parse(s)
f = date.strftime('%d-%b-%G')
puts f

The output is 31-Dec-2999.
This doesn't seem to happen with earlier years.
This happens with Ruby 1.8.6 and JRuby 1.2.0RC2.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-04-10 17:35
(Received via mailing list)
Mark Volkmann <mark@ociweb.com> wrote:

> Why does the year get changed in this code?
>
> require 'date'
> s = '2998-12-31'
> date = Date.parse(s)
> f = date.strftime('%d-%b-%G')

It's because you asked for %G. If you wanted the actual year component
of the date, you would have used %Y. man strftime...

m.
This topic is locked and can not be replied to.