2

I have a ruby on rails app, where the I have a meeting_time that is stored as a datetime type. I want to post it via an ajax call, but JavaScript seems to only have string or object. Is it possible to convert a string into a datetime in JavaScript before posting?

For example, meeting_time: "2016-11-02 02:31:00" posts as a string, or I have tried date=new Date(), but this returns an object.

Thanks!

matisetorm
  • 857
  • 8
  • 21
gwalshington
  • 1,418
  • 2
  • 30
  • 60
  • 1
    Inherently, it will pass as a string in the POST. You will need to convert it from a string to a datetime on the server-side. – Steve Oct 30 '16 at 21:08
  • As far as I understand your question, you want to convert your JavaScript Date object into a Ruby datetime object? For anything else you can create a JavaScript Date object this way: `var date = new Date('2016-11-02 02:31:00');` and you can access its value in multiple ways with the methods of the Date object. However, I recommend to use something like moment.js for date operations in JavaScript. –  Oct 30 '16 at 21:15
  • @Ole with js date object, it creates an object. I need to store the date as the type: datestring. – gwalshington Oct 30 '16 at 21:19
  • I guess this is impossible to do only at the client side. You need to post it as a string to your server and from there you create the datestring object with Ruby. –  Oct 30 '16 at 21:21
  • Ideally, you'd `new Date(date_string).toISOString()` – vol7ron Feb 26 '18 at 17:44

1 Answers1

0

No, this is not possible. You can only pass strings using Ajax. Which makes sense, because a javascript object isn't going to mean much to Ruby. A pretty common way of doing this is to pass JSON to the server and then most languages (including Ruby) can parse the JSON.

Todd Chaffee
  • 6,754
  • 32
  • 41
  • Thanks -- I posted a new question on how to do this in rails, if you want to give that a shot too. http://stackoverflow.com/questions/40345784/take-a-param-in-controller-convert-string-type-and-save – gwalshington Oct 31 '16 at 15:51