Split up days


#1

hi

problem is …

I got date @start and date @stop as a date and I can display like

@start = Thu Jan 19 00:00:00 CST 2006
@stop = Tue Jan 21 00:00:00 CST 2006

how to split up this days and put to array ?

array[] = “Thu Jan 19 00:00:00 CST 2006
Wed Jan 20 00:00:00 CST 2006
Tue Jan 21 00:00:00 CST 2006”

is there some function which can do that ?

thanks for help


#2

Robert K. wrote:

array = [@start, @stop]

Cheers

robert

thank you for answer .
from FORM I can select days, FROM - TO
like from Monday to Friday
exanmple:

from 2006-01-20 to 2006-01-24

and than I need to put each day to database as five days like

2006-01-20
2006-01-21
2006-01-22
2006-01-23
2006-01-24

so first I need to put each day to array and that in loop put each day
to database.

this is what I want
thanks for help


#3

def toSecond(stime)
atime=stime.split("-")
return Time.local(atime[0],atime[1],atime[2]).to_i
end

@start = “2006-01-20”
@stop = “2006-01-24”

array=Array.new
toSecond(@start).step(toSecond(@stop),86400) do |t|
array.push(Time.at(t).strftime("%Y-%m-%d"))
end

puts array

Is this ok?


#4

misiek wrote:

hi

problem is …

I got date @start and date @stop as a date and I can display like

@start = Thu Jan 19 00:00:00 CST 2006
@stop = Tue Jan 21 00:00:00 CST 2006

This is not valid Ruby code. What types are these objects?

how to split up this days and put to array ?

array[] = “Thu Jan 19 00:00:00 CST 2006
Wed Jan 20 00:00:00 CST 2006
Tue Jan 21 00:00:00 CST 2006”

You assign a string but you seem to want an array. Also, you don’t seem
to want to split but to append.

split at whitespace

array = @start.to_s.split /\s+/

split into array, works for Time

array = @start.to_a

put into array

array = [@start, @stop]

concatenate

array = “#{@start}\n#{@stop}”

is there some function which can do that ?

If you tells us what you actually want, we might be able to come up with
an answer.

Cheers

robert

#5

2006/1/24, misiek removed_email_address@domain.invalid:

2006-01-22
2006-01-23
2006-01-24

so first I need to put each day to array and that in loop put each day
to database.

this is what I want
thanks for help

Now we’re cooking. How’s that?

require ‘date’

fetch start and end date from somewhere

start=Date.today
endd=start+30
d=start
while d < endd
puts d
d += 5
end

robert


#6

Sky Y. wrote:

=>2006-01-23
=>2006-01-24

:^)

this is super but why when I will put 2006,3,2 into variable
like @start = “2006,1,3” and @stop = “2006,1,4” does not work ?
days = (Date.new(@start)…Date.new(@stop)).to_a

undefined method `-’ for “2006,1,3”:String


#7

It’s easy to transfer the string “year, month, day” to the parameter
format that meets the requirement of Date.new(year, month, day) :

date_start = @start.split(’,’).map {|x|, x.to_i}
date_stop = @stop.split(’,’).map {|x|, x.to_i}
days = (Date.new(@start)…Date.new(@stop)).to_a

Still elegant?


#8

Range can help you. Example:

require ‘date’
days = (Date.new(2006,1,20)…Date.new(2006,1,24)).to_a
days.each {|date| puts date}
=>2006-01-20
=>2006-01-21
=>2006-01-22
=>2006-01-23
=>2006-01-24

:^)


#9

oops, the last line should be:

days = (Date.new(date_start)…Date.new(date_stop)).to_a


#10

Sky Y. wrote:

date_start = @start.split(’,’).map {|x|, x.to_i}
date_stop = @stop.split(’,’).map {|x|, x.to_i}
days = (Date.new(@start)…Date.new(@stop)).to_a

Still elegant?

I got like

@start = @params[‘start’][‘year’].to_s + “,” +
@params[‘start’][‘month’].to_s + “,” + @params[‘start’][‘day’].to_s

@stop = @params[‘stop’][‘year’].to_s + “,” +
@params[‘stop’][‘month’].to_s + “,” + @params[‘stop’][‘day’].to_s

and I try to put to
days = (Date.new(@start)…Date.new(@stop)).to_a

still got this error undefined method `-’ for “2006,1,3”:String

BTW what is “x” => date_start = @start.split(’,’).map {|x|, x.to_i} ?
^ ^

or is going about (’,’) ? hmm

because is error
…/script/…/config/…/app/controllers/hour_controller.rb:178: parse
error, unexpected ‘,’
date_start = @start.split(’,’).map {|x|, x.to_i}
^


#11

I removed , from date_start = @start.split(’,’).map {|x| x.to_i}
^

now got
private method `split’ called for Tue Jan 24 00:00:00 CST 2006:Time


#12

ups to put to

date_start = @start.split(’,’).map {|x|, x.to_i}

I try format like this

@start =
Time.local(@params[‘start’][‘year’],@params[‘start’][‘month’],@params[‘start’][‘day’])

the same error :frowning:


#13

sry, I copied a wrong part of my own code. What I tried to do is to
split the string “year, month, day” into 3 integers and store in an
array [year, month, day], then create Date objects from the arrays.

def new_date(date)
Date.new(date[0], date[1], date[2])
end

date_start = @start.split(’,’).map {|x|, x.to_i}
date_stop = @stop.split(’,’).map {|x|, x.to_i}
days = (new_date(date_start)…new_date(date_stop)).to_a

This should work now.