Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Date with ruby

This is my date 20100816 and it integer date.

How to display it like 08/16/2010?

like image 464
krunal shah Avatar asked Mar 12 '26 13:03

krunal shah


2 Answers

You can use the Date.strptime method to read a date in a given format and strftime to print it out in another format:

require 'date'
intdate = 20100816
Date.strptime(intdate.to_s, "%Y%m%d").strftime("%m/%d/%Y")
#=> "08/16/2010"
like image 81
sepp2k Avatar answered Mar 14 '26 05:03

sepp2k


You can use the Date.parse method and strftime to format the date

Date.parse(20100816.to_s).strftime("%m/%d/%Y")
like image 45
Brent M Avatar answered Mar 14 '26 05:03

Brent M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!