Search code examples
reactjsdatepickergetaxiosfetch

How to download data from server using date in get methos React?


I'm new with React. I was looking for information for several days to no avail. So I decided to ask you my stupid question.

I have problem with transforming date from DatePicker to date format which than I can use in get request. I use DatePicker from this https://www.npmjs.com/package/react-datepicker

Sample correct api call:

localhost:8080/api/measurement/12374?date=2020-12-13 12:00

but date from DatePicker look like:

Sun Dec 13 2020 12:00:00 GMT+0100 (Central European Standard Time)

I play with date for example:

let x = new Date();
let x1 = x.toLocaleDateString() + " " + x.toLocaleTimeString();
output: 23/12/2020 09:29:16

but still I have problem with date format and I also don't know how to pass this date correclty to get method, because http request have % instead "space"

http://localhost:8080/api/measurement/12120?date=23/12/2020%2009:48:15

but when I use string as date it work fine and download data:

let value = "12374";
let date = "2020-12-13 12:00";
const request = "http://localhost:8080/api/measurement/" + value + "?date=" + date;

Can someone explain me how can I convert date from DatePicker to format like this 2020-12-13 12:00 and then use it in get method?

I will be very grateful for any answer! Thank you ;)


Solution

  • you can use momentjs for this task to format date. https://www.npmjs.com/package/moment

    import moment from 'moment'
    
    let date = new Date();
    let result = moment(date).format('DD/MM/YYYY hh:mm')
    

    output: 23/12/2020 02:23