Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong date with ruby Date.today and DateTime.now

I've installed ruby-1.8.6-p383 with RVM. System ruby is 1.9.1_p378-1

I'm getting the wrong date from Date.today and DateTime.now when using ruby 1.8.. Whereas Time.now is correct:

irb(main):002:0> DateTime.now.to_s
=> "2126--1-10618T11:23:43+00:00"
irb(main):004:0> Date.today.to_s
=> "2126--1-10618"
irb(main):005:0> Time.now
=> Thu Jan 28 11:55:27 +0000 2010

All is well if I switch to ruby 1.9:

irb(main):003:0> DateTime.now.to_s
=> "2010-01-28T11:58:51+00:00"
irb(main):004:0> Date.today.to_s
=> "2010-01-28"
irb(main):005:0> Time.now
=> 2010-01-28 11:59:05 +0000

Any advice on how to get DateTime to work properly in ruby 1.8 would be most appreciated!

like image 976
Rob Avatar asked Jan 28 '10 12:01

Rob


People also ask

What does DateTime now return in Ruby?

DateTime#now() : now() is a DateTime class method which returns a DateTime object denoting the given calendar date. Return: DateTime object denoting the given calendar date.

How do I change the Date format in Ruby?

Two steps: You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.


1 Answers

I stumbled upon similar problem and got 3 different solutions, which i tried 2 of and both worked. Read more about it from here:

http://www.ruby-forum.com/topic/210647

Jarmo

like image 141
Jarmo Pertman Avatar answered Sep 28 '22 07:09

Jarmo Pertman