0

The error is bad request 400, data is this: data {\"id\":2,\"name\":\"Tuesday\",\"from\":\"21:16:09\",\"to\":\"23:16:06\"}" I have issues converting dateTime to time in reactjs with axios. I have no issues on Postman, but with axios i can't convert: Here is the pre-request script on Postman:

var moment = require("moment");

pm.environment.set('from',moment().add(14, 'hours').toISOString());
pm.environment.set('to',moment().add(30, 'hours').toISOString());

Here is the form in ReactJs:

<Form onSubmit={handleSubmit} autoComplete='off'>
                <Form.Input placeholder='Day' value={workinghour.name} name='name' onChange={handleInputChange}/>
                <Form.Input type='time' step='2' placeholder='From' value={workinghour.from} name='from' onChange={handleInputChange}/>
                <Form.Input type='time' step='2' placeholder='To' value={workinghour.to} name='to' onChange={handleInputChange}/>

And i use this to split the datetime and to get just time:

useEffect(() => {
        agent.WorkingHours.list().then(response => {
            let workinghours = [];
            response.forEach(workinghour => {
                workinghour.from = workinghour.from.split('T')[1]
                workinghour.from = workinghour.from.split('.')[0];
                workinghour.to = workinghour.to.split('T')[1];
                workinghour.to = workinghour.to.split('.')[0];
                workinghours.push(workinghour);
            })
            setWorkingHours(workinghours);
        })
    }, [])
  • There is no `time` key in moment.js `.add()`.... Is that a typo? Maybe it should be `days`? [moment.js add() documentation](https://momentjs.com/docs/#/manipulating/add/) – Louys Patrice Bessette Jul 10 '21 at 23:10
  • @LouysPatriceBessette i changed it into hour, it still works on postman, but with axios it doesnt convert to datetime – programmer1234 Jul 10 '21 at 23:16
  • 1
    Put a `console.log('workinghour.from =', workinghour.from);` and the same for the `to` in your `handleSubmit()` to show exactly what's getting sent to the server. In your Postman script you are explicitly converting to ISO format before sending; whatever is getting sent by axios is probably not ISO format or anything like it. – Neal Burns Jul 10 '21 at 23:45
  • @NealBurns workinghour.from = 21:16:09 workinghour.to = 23:16:06 this is showing in console, also i wrote what data shows in console, data: "{\"id\":2,\"name\":\"Tuesday\",\"from\":\"21:16:09\",\"to\":\"23:16:06\"}" – programmer1234 Jul 11 '21 at 00:06

1 Answers1

0

I took this from your Postman script and ran it in the browser console:

> moment().add(30, 'hours').toISOString()
> "2021-07-12T07:40:15.936Z"

The format you are sending via axios is this: "21:16:09", which is missing the date, fractional seconds, time zone suffix ("Z" for UTC), and the letter "T" between the date and time.

"The JSON value could not be converted to System.DateTime" <-- Is this coming from ASP.NET? If that's the case, ASP.NET is trying to make a DateTime, which is both a date and a time.

Take a look at previous discussion on this topic and you can probably take it from here. If you get stuck, leave a comment and I'll write back.

Neal Burns
  • 839
  • 4
  • 5