How to convert Date to this json format

Hi
I have to create a json date like

{“startDate”:"/Date(1291145744713-0700)/",“endDate”:"/Date(1293824144713-0700)/"}

    This is just an example.

My case startdate = Time.now-2.day
enddate = Time.now

  I dont know how to convert this to the above format. Please help

Thanks

date_range = {:start_date => (Time.now - 2.days).to_i, :end_date =>
Time.now.to_i}

date_range.to_json will give
{“start_date”:1314182763,“end_date”:1314355563}

Is this what you want?

t = Time.now
puts t.strftime(“%s%z”)

format = ‘{“startDate”:“/Date(%s)/”}’
puts format % [t.strftime(“%s%z”)]

–output:–
1314356117-0400
{“startDate”:“/Date(1314356117-0400)/”}

The %s, %z come from the docs for strftime() here:

http://www.ruby-doc.org/core/classes/Time.html#M000392

The %s is a little confusing because it has two different meanings in
the code:

  1. %s => an argument to strftime(), convert the Time object to seconds
  2. %s => a placeholder in a String for a substitution