Convert String to date

Discussion in 'Ruby' started by aidy, Sep 1, 2006.

  1. aidy

    aidy Guest

    Hi,

    I have a string object

    "27/07/06"

    that is referenced in this variable: start_date

    what I want to do is convert this string into a date, and add a day to
    it.

    Something like this

    start_date = start_date.to_d
    end_date = start_date + 1

    but I cannot see a to_date method. Could anyone please help?

    Aidy
     
    aidy, Sep 1, 2006
    #1
    1. Advertising

  2. aidy

    Paul Battley Guest

    On 01/09/06, aidy <> wrote:
    > I have a string object
    >
    > "27/07/06"
    >
    > that is referenced in this variable: start_date
    >
    > what I want to do is convert this string into a date, and add a day to
    > it.
    >
    > Something like this
    >
    > start_date = start_date.to_d
    > end_date = start_date + 1
    >
    > but I cannot see a to_date method. Could anyone please help?


    require 'date'
    date = Date.strptime(start_date, '%d/%m/%y')
    date.year # => 2006
    date.month # => 7
    date.mday # => 27
    (date + 1).mday # => 28

    Paul.
     
    Paul Battley, Sep 1, 2006
    #2
    1. Advertising

  3. aidy

    aidy Guest

    Hi Paul
    > require 'date'
    > date = Date.strptime(start_date, '%d/%m/%y')
    > date.year # => 2006
    > date.month # => 7
    > date.mday # => 27
    > (date + 1).mday # => 28
    >


    Thanks for getting back.

    I have extrapolated your example (see below)

    require 'date'
    start_date = Date.strptime(start_date, '%d/%m/%Y')
    end_day = (start_date + 1).mday
    end_month = (start_date).mon
    end_year = (start_date).year
    end_date = Date.new(end_year, end_month, end_day)
    p "the end_date is: #{end_date}"

    However, I am having a couple of difficulties

    1) Now for some reason the year '2006' is putting to the console

    "the end_date is: 0006-07-28"


    2) And if I add this line of code

    end_date = Date.strptime(end_date, '%d/%m/%Y')

    I receive the error the #strptime is a private method!

    Using Ruby 184.17

    Could you please help?

    Thanks

    aidy
     
    aidy, Sep 1, 2006
    #3
  4. aidy

    Paul Battley Guest

    On 01/09/06, aidy <> wrote:
    > Thanks for getting back.
    >
    > I have extrapolated your example (see below)
    >
    > require 'date'
    > start_date = Date.strptime(start_date, '%d/%m/%Y')
    > end_day = (start_date + 1).mday
    > end_month = (start_date).mon
    > end_year = (start_date).year
    > end_date = Date.new(end_year, end_month, end_day)
    > p "the end_date is: #{end_date}"


    You missed a simple point: adding one to a date gives you the next day:

    require 'date'
    start_date = '27/07/06'
    start_date = Date.strptime(start_date, '%d/%m/%y')
    end_date = start_date + 1
    puts "The end date is #{ end_date }."

    outputs:

    The end date is 2006-07-28.

    > However, I am having a couple of difficulties
    >
    > 1) Now for some reason the year '2006' is putting to the console
    >
    > "the end_date is: 0006-07-28"


    That's because you used %Y, not %y, in strptime. If you are parsing a
    two-digit year, you need to use %y to infer the century automatically.
    %Y reads it verbatim: 06 is 0006, not 2006.

    > 2) And if I add this line of code
    >
    > end_date = Date.strptime(end_date, '%d/%m/%Y')
    >
    > I receive the error the #strptime is a private method!


    Is the first parameter that you pass to strptime a String or a Date?
    If it's the Date calculated in the earlier example, that will fail
    with an error 'private method `sub!' called for #<Date:...>'. And if
    it's already a Date, there's no need to use strptime!

    Paul.
     
    Paul Battley, Sep 1, 2006
    #4
  5. aidy

    aidy Guest

    Hi Paul

    > And if it's already a Date, there's no need to use strptime!


    If I write this

    require 'date'
    start_date = Date.strptime(start_date, '%d/%m/%y')
    end_day = (start_date + 1)
    p end_day.to_s #putting to string to see format

    I recieve this
    => "2006-06-15"

    How then do I format it into a two-digit year European date?

    e.g.

    "15/06/06"

    (I can't see anything in the date class.)

    This is what I am doing at the moment

    require 'date'
    start_date = Date.strptime(start_date, '%d/%m/%y')
    end_day = (start_date + 1)
    p end_day.to_s
    end_day = end_day.to_s
    a = end_day.split('-')
    end_day = "#{a[2]}/#{a[1]}/#{a[0]}"
    p end_day

    => "15/06/2006"

    thanks

    aidy
     
    aidy, Sep 1, 2006
    #5
  6. aidy

    Paul Battley Guest

    On 01/09/06, aidy <> wrote:
    > p end_day.to_s #putting to string to see format
    >
    > I recieve this
    > => "2006-06-15"
    >
    > How then do I format it into a two-digit year European date?


    end_day.strftime('%d/%m/%y')
    => "15/06/06"

    Paul.
     
    Paul Battley, Sep 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. h_ghanaty

    convert gregorian date to jalay date

    h_ghanaty, Nov 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    553
    Bret Mulvey [MS]
    Nov 9, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,380
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    617
    Roedy Green
    Jul 8, 2004
  4. Replies:
    8
    Views:
    823
    Boris
    May 1, 2007
  5. davyb
    Replies:
    7
    Views:
    226
    Gregory Brown
    Nov 18, 2005
Loading...

Share This Page