1
votes

Date format looks like that: 2020-07-11 23:11:52

I am taking data from csv file using fetch and splitting it into single data

How to change this string (date) into date with time to plot on the chart

    async function getData(){

        const response =await fetch('data.csv');
        const data = await response.text();
        const table = data.split('\n').slice(1);
        table.forEach(row =>{
            const columns = row.split(',');
            const date = columns[0];
         //   const dayshours = date.split(' ');
         //   const days = dayshours[0];
            xlabels.push(date);
            const temp = columns[1];
            ytemps.push(temp);
            console.log(date, temp);

        });


    }


1

1 Answers

0
votes

Map the numbers into a string in the ISO standard, which looks like '2020-08-10T22:36:25.772'

So in your case it would look like '2020-07-11T23:11:52.000'. Note that it looks pretty much identical to what you've already got, except the T between the date and the time of day, and append .000 to the end to set the milliseconds component to zero.

Then take your string and use new Date('2020-07-11T23:11:52.000')

enter image description here