Search code examples
javascriptarraysdata-cleaning

Formating date returns NaN


I made a function to clean null values but it doesnt working as well as i expected. This is my change date function.

This function take generated date from thingspeak and change it for better reading.I added option for short version of date and longer.

function getDate(oldDate, extended) {
  const date = new Date(oldDate);
  const d = date.getDate();
  let m = date.getMonth() + 1;
  const y = date.getFullYear();
  const h = date.getHours();
  let min = date.getMinutes();

  if (m < 10) {
    m = "0" + m;
  }
  if (min < 10) {
    min = "0" + min;
  }
  let convertedDate;

  extended
    ? (convertedDate = `${h}:${min} ${d}/${m}/${y}`)
    : (convertedDate = `${d}/${m}/${y}`);

  return convertedDate;
}

And this is fucntion I made to clean data and change date.It takes dirty data and remove this with null values then it send changed version previously feed[i].created_at = getDate(feed[i].created_at); was in firts loop between splice funtion and subtraticting but i was getting error.

function cleanData(obj) {
  if (obj) {
    const feed = obj.feeds;

    for (let i = 0; i < feed.length; i++) {
      if (feed[i].field1 === null || feed[i].field2 === null) {
        feed.splice(i, 1);
        i--;
      }
    }

    if (feed) {
      for (let i = 0; i < feed.length; i++) {
        feed[i].created_at = getDate(feed[i].created_at);
      }
    }

    return feed;
  }
}

I dont know what i doing bad but i get this output

[
    {
        "created_at": "NaN/NaN/NaN",
        "field1": "366.9",
        "field2": "888.1"
    },
    {
        "created_at": "NaN/NaN/NaN",
        "field1": "22.3",
        "field2": "56.625"
    }
]

and i post dirty data as well

{

        "channel": {
            "id": 1353918,
            "name": "Air quality meter",
            "latitude": "0.0",
            "longitude": "0.0",
            "field1": "PM25",
            "field2": "PM10",
            "created_at": "2021-04-09T20:51:56Z",
            "updated_at": "2021-04-09T20:51:56Z",
            "last_entry_id": 208
        },
        "feeds": [
            {
                "created_at": "2021-05-22T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-23T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-24T00:00:00Z",
                "field1": "366.9",
                "field2": "888.1"
            },
            {
                "created_at": "2021-05-25T00:00:00Z",
                "field1": "22.3",
                "field2": "56.625"
            },
            {
                "created_at": "2021-05-26T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-27T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-28T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-29T00:00:00Z",
                "field1": null,
                "field2": null
            },
            {
                "created_at": "2021-05-29T00:00:00Z",
                "field1": null,
                "field2": null
            }
        ]
    }

For now only date formating doesnt work however I tested exacly same code in codesandbox and it works https://codesandbox.io/s/beautiful-bird-cb0cy?file=/src/index.js:1374-1390. If someone have anserwer to this questin pleas write it . Thanks


Solution

  • If you look at the Date documentation, you will find that the date parsing will differ depending on the host machine. This explains why it works in codesandbox and not in your machine.

    Quoting it:

    parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (...) There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

    My recommendation is to use Momentjs to parse and handle the date. It also helps you format your output, so you don't have to do it yourself:

    function getDate(oldDate, extended) {
      const date = moment(oldDate);
      if (!date.isValid()) { throw new Error(`Invalid date: ${oldDate}`); }
      
      if(extended) {
        return date.format("HH:mm DD/MM/YYYY");
      } else {
        return date.format("DD/MM/YYYY");
      }
    }