Topic: How to convert a string to a date?

Seems simple enough to do but I can't figure this out.
I'm using UTC as my time zone.
I am trying to convert 31/01/2010 to a date but I can't seem to figure out how to do this.
It's a pretty standard format  (dd mm yyyy)

d = Date.parse("31/01/2010")

produces an invalid date error

Any ideas?

Cheers

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to convert a string to a date?

Date.parse tries to be clever, so probably best to tell Ruby your date format instead:

Date.strptime("31/01/2010", "%d/%m/%Y")
Rob Anderton
TheWebFellas

Re: How to convert a string to a date?

Rob, That's fantastic and just what I was looking for.
Thank you :-)

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to convert a string to a date?

Not so much 'clever' as 'defaults to american format' really.   Oddly enough, if you do it with hyphens instead of forward slashes:

Date.parse("31-01-2010")

then it does it the way a british person (and maybe everybody else in the world?) would expect.  I always thought this was a bit random, the api makes no mention of it anyway.

(Regardless, rob's method is safer and more readable)

Last edited by Max Williams (2010-03-23 08:13:37)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: How to convert a string to a date?

Here's the cleverness i referred to big_smile

# File lib/date/format.rb, line 958
  def self._parse(str, comp=false)
    str = str.dup

    e = Format::Bag.new

    e._comp = comp

    str.gsub!(/[^-+',.\/:0-9@a-z\x80-\xff]+/in, ' ')

    _parse_time(str, e) # || _parse_beat(str, e)
    _parse_day(str, e)

    _parse_eu(str, e)     ||
    _parse_us(str, e)     ||
    _parse_iso(str, e)    ||
    _parse_jis(str, e)    ||
    _parse_vms(str, e)    ||
    _parse_sla_us(str, e) ||
    _parse_iso2(str, e)   ||
    _parse_year(str, e)   ||
    _parse_mon(str, e)    ||
    _parse_mday(str, e)   ||
    _parse_ddd(str, e)

    if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/in, ' ')
      if e.year
        e.year = -e.year + 1
      end
    end

    if str.sub!(/\A\s*(\d{1,2})\s*\z/n, ' ')
      if e.hour && !e.mday
        v = $1.to_i
        if (1..31) === v
          e.mday = v
        end
      end
      if e.mday && !e.hour
        v = $1.to_i
        if (0..24) === v
          e.hour = v
        end
      end
    end

    if e._comp and e.year
      if e.year >= 0 and e.year <= 99
        if e.year >= 69
          e.year += 1900
        else
          e.year += 2000
        end
      end
    end

    e.offset ||= zone_to_diff(e.zone) if e.zone

    e.to_hash
  end

As you can see it tries all kind of formats, crazy stuff eh?! If you really want to blow your mind you can see the internals of each of those _parse_xxx methods here: http://github.com/shyouhei/ruby/blob/tr … /format.rb

Rob Anderton
TheWebFellas

Re: How to convert a string to a date?

See the FORMAT text function in VBA. It works very much like the TEXT Excel function.

i.e.
MYSTRING=FORMAT(NOW(),"yyyymmdd")