Problems with Datetime in Javascript with C#

Asked

Viewed 77 times

0

Staff I have a class that gets a start and end date from a Javascript:

public object getTime(DateTime inicio, DateTime fim) { ... }

I’m going through the dates like this through Javascript:

$.getJSON(`services/api/tempo/getTime/${start.format('YYYY-MM-DD')}/${finish.format('YYYY-MM-DD')}`) { ... }

But I must pass this way:

$.getJSON(`services/api/tempo/getTime/${start.format('YYYY-MM-DDTHH:mm')}/${finish.format('YYYY-MM-DDTHH:mm')}`) { ... }

But this way is making a mistake

GET http://localhost/services/api/tempo/getTime/2020-06-02T14:10/2020-06-02T14:20 400 (Bad Request)

Has anyone ever faced such a thing? Could you please help with this problem?

  • 1

    Error where, on the front on the back? Which error message? Made a console.log of start.format('YYYY-MM-DDTHH:mm') and of Finish.format('YYYY-MM-DDTHH:mm') ?

  • Front error, I edited the question and added the error.

  • error is not defined already says, you are trying to access a variable called error that was not declared, so your error message has nothing to do with date.format and nor with Datetime, has to do with a loose variable not defined and clear, the codes presented in the question have nothing to do with this.

  • The same problem is the first error. This error is a var that I took from the code and forgot to take from the return. But the same problem is that bad request. This Bad Request only happens when I use the date in this informed way.

  • Okay, this giving bad request, means that the debugger is off or that you are not looking at the Devtools network console or not looking at the visualstudio (if the test is in place) the exceptions, there is no way to guess, Another thing, I asked two questions and I’ll be forced to repeat them: Did a start.format('YYYY-MM-DDTHH:mm') and Finish.format('YYYY-MM-DDTHH:mm') console? What do they return? Understand that to help you you need to provide these details, after all it is the least. I am waiting to help you.

  • I did yes returns the dates 2020-06-02T16:30... I made the URI Meeting quoted below and returned 2020-06-02T16%3A01, but Ianda is with bad request.

  • Okay, but in Visualstudio, does the error breakpoint display the details at the moment? Is Exception generated or not? As I said, and I already asked you 4 questions that you did not answer any, give the details, we can not guess important details

Show 2 more comments

1 answer

1

Probably missing the "escape" in the URL, because some values in the URL should be encoded, this should solve:

let a = encodeURIComponent(start.format('YYYY-MM-DDTHH:mm'));
let b = encodeURIComponent(finish.format('YYYY-MM-DDTHH:mm'));

$.getJSON(`services/api/tempo/getTime/${a}/${b}`) { ... }
  • I made the quoted URI Encounter returned 2020-06-02T16%3A01, but walking is given bad request 400.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.