Converting Dates Question

5 views
Skip to first unread message

Moochie

unread,
Oct 9, 2008, 12:08:02 PM10/9/08
to Watir General

How can I convert the following dateformate?

puts "2007-12-30".strftime("%m/%d/%Y")
puts "2007-12-30".to_a.strftime("%m/%d/%Y")

The I've pulled the date from the database, but I want to format as
12/30/2007.

Tiffany Fodor

unread,
Oct 9, 2008, 12:44:52 PM10/9/08
to Watir General
There may be a slicker way to do it, but here's a method I wrote to
solve the same problem.

def dateString(date)

dateArray = Array.new
dateArray = date.split('-')
dateYear = dateArray[0]
dateMonth = dateArray[1]
dateDay = dateArray[2]

date = "#{dateMonth}/#{dateDay}/#{dateYear}"

return date
end

Jason Trebilcock

unread,
Oct 9, 2008, 1:03:14 PM10/9/08
to watir-...@googlegroups.com
Doing it through SQL:

select date_format(now(), '%m/%d/%Y')

Replace now() with your variable and give it a whirl.

andy sipe

unread,
Oct 9, 2008, 1:10:04 PM10/9/08
to watir-...@googlegroups.com
If the value you are getting from the database is a string, try this:

irb(main):004:0> require 'date'                                            => true
irb(main):005:0> dt = DateTime.parse('2007-12-30')             => #<DateTime: 4908929/2,0,2299161>
irb(main):006:0> dt.year                                                   => 2007
irb(main):007:0> dt.day                                                    => 30
irb(main):008:0> dt.month                                                => 12
irb(main):010:0> str = dt.strftime('%m/%d/%Y')                  => "12/30/2007"

If the value you are getting isn't a string then you'll either have to convert it to a Date or a string and go from there.

-andy

Moochie

unread,
Oct 9, 2008, 2:26:01 PM10/9/08
to Watir General

puts DateTime.parse('2007-12-30').strftime('%m/%d/%Y')

Worked, Thanks Andy.

Jason, You're suppose to be on vacation. Step away from the computer.

Also, I tried that one. Thanks though.

DD

On Oct 9, 12:10 pm, "andy sipe" <ajs.gene...@gmail.com> wrote:
> If the value you are getting from the database is a string, try this:
>
> irb(main):004:0> require 'date'
> => true
> irb(main):005:0> dt = DateTime.parse('2007-12-30')             =>
> #<DateTime: 4908929/2,0,2299161>
> irb(main):006:0> dt.year
> => 2007
> irb(main):007:0> dt.day
> => 30
> irb(main):008:0> dt.month                                                =>
> 12
> irb(main):010:0> str = dt.strftime('%m/%d/%Y')                  =>
> "12/30/2007"
>
> If the value you are getting isn't a string then you'll either have to
> convert it to a Date or a string and go from there.
>
> -andy
>
> On Thu, Oct 9, 2008 at 1:03 PM, Jason Trebilcock <jason.trebilc...@gmail.com
>
>
>
> > wrote:
> > Doing it through SQL:
>
> > select date_format(now(), '%m/%d/%Y')
> > Replace now() with your variable and give it a whirl.
>
> > On Thu, Oct 9, 2008 at 11:08 AM, Moochie <dduph...@redbrickhealth.com>wrote:
>
> >> How can I convert the following dateformate?
>
> >> puts "2007-12-30".strftime("%m/%d/%Y")
> >> puts "2007-12-30".to_a.strftime("%m/%d/%Y")
>
> >> The I've pulled the date from the database, but I want to format as
> >> 12/30/2007.- Hide quoted text -
>
> - Show quoted text -

Željko Filipin

unread,
Oct 9, 2008, 4:27:16 PM10/9/08
to watir-...@googlegroups.com
On Thu, Oct 9, 2008 at 8:26 PM, Moochie <ddup...@redbrickhealth.com> wrote:
> Jason, You're suppose to be on vacation.  Step away from the computer.

Too good not to comment! :)

Željko

Jason Trebilcock

unread,
Oct 9, 2008, 5:08:52 PM10/9/08
to watir-...@googlegroups.com
As much as I'd like to have been on vacation, it was only training.  And now I'm back in the office...so there!  But, I think someone needs to schedule a meeting or two or ten to go over all the crap that's going to be dumped in my lap...er, passed along, even.  This means you, Mr Moochie! :-)

(And y'know, I really should just throw something over the cube wall.  It'd save bandwidth.)

Jason
Reply all
Reply to author
Forward
0 new messages