0

I am using react js. I have some json data where I am fetching the timestamp. It has date and time both. For example:

timestamp":"2020-03-23T14:00:00.000Z"

Now after fetching all the json data including timestamp. I wanna make a chart, but I only want to use the date in my chart, not the time. How do I only get the date from the timestamp in the form of 2020/03/23 not the- but with /? I am using chartjs for making the chart Thanks.

Edit:

for (const dataobj of json) {
      let tempsymbolsDate = dataobj.timestamp.split("T")[0];
      tempsymbolsDate.replace("-", "/"); //here it doesn't replace with `/` 
      console.log(tempsymbolsDate);
    }
Hemlata
  • 65
  • 1
  • 8

1 Answers1

1

You can split the string using "2020-03-23T14:00:00.000Z".split("T")[0] to get the date without the time.

To replace - characters with /, use the str.replace(searchvalue, newvalue) method. For example: "2020-03-23".replace(/-/g, "/")

Edit:

for (const dataobj of json) {
      let tempsymbolsDate = dataobj.timestamp.split("T")[0];
      tempsymbolsDate = tempsymbolsDate.replace(/-/g, "/"); 
      console.log(tempsymbolsDate);
    }

Edit 2:

for (const dataobj of json) {
      let tempsymbolsDate = dataobj.timestamp.split("T")[0];
      tempArray = tempsymbolsDate.split("-");
      tempsymbolsDate = tempArray[2] + "/" + tempArray[1] + "/" + tempArray[0];
      console.log(tempsymbolsDate);
    }
Green-Avocado
  • 901
  • 5
  • 20
  • @Hemlata That's strange, what happens when you try running that bit of code? – Green-Avocado May 13 '20 at 05:06
  • `/-/` turns red in my IDE and in console the date doesn't change the format – Hemlata May 13 '20 at 05:08
  • @Hemlata That's odd, if the problem is with the `/-/` bit, can you try running `"2020-03-23".replace("-","/").replace("-","/")` ? – Green-Avocado May 13 '20 at 05:10
  • I have tried that as well, but it doesn't work and instead print the date with 2020-03-23 format. I have edited my question and added some of my code if that helps. – Hemlata May 13 '20 at 05:12
  • @Hemlata Can I see a code snippet? Is it possible that you're printing a variable and haven't set the variable to the return value of the replace method? – Green-Avocado May 13 '20 at 05:14
  • @Hemlata Ah I see, you're calling the method, but that doesn't actually change the string. Try replacing that line with `tempsymbolsDate = tempsymbolsDate.replace(/-/g, "/");` – Green-Avocado May 13 '20 at 05:16
  • @Hemlata I find it very strange that it doesn't seem to work, as it has been working when I tried running it in the console. I've edited my response, can you try copy-pasting that to ensure that nothing is missing still? – Green-Avocado May 13 '20 at 05:37
  • Yess, it's working now. Can I also change it to format date/month/year instead of year/month/date? – Hemlata May 13 '20 at 05:42
  • @Hemlata Yes, I've edited the solution to store the numbers as an array, then print the array in reverse order. See if that works. – Green-Avocado May 13 '20 at 05:48