How to convert Date to this json format

I have to create a json date like


    This is just an example.

My case startdate =
enddate =

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


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

date_range.to_json will give

Is this what you want?

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

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


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

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