date formatting bug?

M

Mark Volkmann

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

matt neuburg

Mark Volkmann said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top