How to getDate

Hi all,

I’ve a string “Mon Nov 20 00:00:00 UTC+0530 2006” in my database.
I want to retrieve that in dd-mm-yyyy format.
Please help me out.

Thanks,

time.strftime( string ) => string

        Formats time according to the directives in the given

format string. Any text not listed as a directive will be passed
through to the output string.

        Format meaning:

          %a - The abbreviated weekday name (``Sun'')
          %A - The  full  weekday  name (``Sunday'')
          %b - The abbreviated month name (``Jan'')
          %B - The  full  month  name (``January'')
          %c - The preferred local date and time representation
          %d - Day of the month (01..31)
          %H - Hour of the day, 24-hour clock (00..23)
          %I - Hour of the day, 12-hour clock (01..12)
          %j - Day of the year (001..366)
          %m - Month of the year (01..12)
          %M - Minute of the hour (00..59)
          %p - Meridian indicator (``AM''  or  ``PM'')
          %S - Second of the minute (00..60)
          %U - Week  number  of the current year,
                  starting with the first Sunday as the first
                  day of the first week (00..53)
          %W - Week  number  of the current year,
                  starting with the first Monday as the first
                  day of the first week (00..53)
          %w - Day of the week (Sunday is 0, 0..6)
          %x - Preferred representation for the date alone, no

time
%X - Preferred representation for the time alone, no
date
%y - Year without a century (00…99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%’’ character

           t = Time.now
           t.strftime("Printed on %m/%d/%Y")   #=> "Printed on

04/09/2003"
t.strftime(“at %I:%M%p”) #=> “at 08:56AM”

time.strftime( string ) => string

           t = Time.now
           t.strftime("Printed on %m/%d/%Y")   #=> "Printed on

04/09/2003"
t.strftime(“at %I:%M%p”) #=> “at 08:56AM”

Hi,
Yes, You are right, I can format using time.strftime(…
But my string “Mon Nov 20 00:00:00 UTC+0530 2006” is of varchar type.
So first I have to convert it to date/time format and then use strftime
method.
How can do that.
Thanks,

Rahul Ha wrote:

time.strftime( string ) => string

           t = Time.now
           t.strftime("Printed on %m/%d/%Y")   #=> "Printed on

04/09/2003"
t.strftime(“at %I:%M%p”) #=> “at 08:56AM”

Hi,
Yes, You are right, I can format using time.strftime(…
But my string “Mon Nov 20 00:00:00 UTC+0530 2006” is of varchar type.
So first I have to convert it to date/time format and then use strftime
method.
How can do that.
Thanks,

Hi,
I found a method strptime that converts string to date in req. format.
But it works for string like ‘27/07/06’. How can convert “Mon Nov 20
00:00:00 UTC+0530 2006” string to date 20-11-2006 form.
############
require ‘date’
start_date = ‘27/07/06’
start_date = Date.strptime(start_date, ‘%d/%m/%y’)
#################
please help me out.
Thanks,

irb(main):023:0> start_date = “Mon Nov 20 00:00:00 UTC+0530 2006”
=> “Mon Nov 20 00:00:00 UTC+0530 2006”
irb(main):025:0> d= Date.strptime(start_date, ‘%a %b %d %H:%M:%S %Z %Y’)
=> #<Date: 4908119/2,0,2299161>
irb(main):026:0> d.strftime

=> “2006-11-20”


Hi David,
Yes It worked, Many thanks. Can we change the format and display it in
dd-mm-yyyy format ? also can we do it reversely,
like “2006-11-20” date to “Mon Nov 20 00:00:00 UTC+0530 2006” ?

Thanks and regards

irb(main):023:0> start_date = “Mon Nov 20 00:00:00 UTC+0530 2006”

=> “Mon Nov 20 00:00:00 UTC+0530 2006”

irb(main):025:0> d= Date.strptime(start_date, ‘%a %b %d %H:%M:%S %Z %Y’)

=> #<Date: 4908119/2,0,2299161>

irb(main):026:0> d.strftime

=> “2006-11-20”

Regards,
Dave


Information and Educational Technology
Kwantlen University College - 604-599-2120
“So powerful is the light of unity that it can illuminate the whole
earth.” --Bahá'u’lláh

Rahul Ha [email protected]
Sent by: [email protected]
08-05-2007 02:02 AM
Please respond to
[email protected]

To
[email protected]
cc

Subject
[Rails] Re: How to getDate

Rahul Ha wrote:

So first I have to convert it to date/time format and then use strftime
method.
How can do that.
Thanks,

Hi,
I found a method strptime that converts string to date in req. format.
But it works for string like ‘27/07/06’. How can convert “Mon Nov 20
00:00:00 UTC+0530 2006” string to date 20-11-2006 form.
############
require ‘date’
start_date = ‘27/07/06’
start_date = Date.strptime(start_date, ‘%d/%m/%y’)
#################
please help me out.
Thanks,


Posted via http://www.ruby-forum.com/.

Sure. You can get it back with
d.strftime(’%a %b %d %H:%M:%S %Z %Y’)

require ‘date’

t = ‘Mon Nov 20 00:00:00 UTC+0530 2006’
d= Date.strptime(t, ‘%a %b %d %H:%M:%S %Z %Y’)
date = d.to_s
year, month, day = date.split(/-/)
month = month.sub(/^0/,’’)
puts day + “/” + month + “/” + year

puts d.strftime(’%a %b %d %H:%M:%S %Z %Y’)

Something screwy with the time zone name though…

Dave

Yes It worked, Many thanks. Can we change the format and display it in

dd-mm-yyyy format ? also can we do it reversely,
like “2006-11-20” date to “Mon Nov 20 00:00:00 UTC+0530 2006” ?

Hi ,
I’ve tried it like,
##################
t = ‘Mon Nov 20 00:00:00 UTC+0530 2006’
d= Date.strptime(t, ‘%a %b %d %H:%M:%S %Z %Y’)
date = d.to_s
year, month, day = date.split(/-/)
month = month.sub(/^0/,’’)
puts day + “/” + month + “/” + year
##########################
==> 20/11/2006

It is working. Can we convert it again to “Mon Nov 20 00:00:00 UTC+0530
2006” ?
:~}

Regards,