2

Here I'm getting my date response and storedin local variable responsedata and mapped from customdata I want to cover that date coming from i.logs[0].datetime to local time string! just stored all data in apidata state so I can forward them as props. the main question in datetime here im getting a date in unix formate but i want to conver it into local time!!

  useEffect(() => {
    const postdata = {
      api_key: apiKey,
      format: "json",
      logs: 1,
      custom_uptime_ranges: "1658892600_1658979000-1658946600_1659033000",
    };
    const url = "https://api.uptimerobot.com/v2/getMonitors";
    const getData = async () => {
      await fetch(url, {
        method: "POST",
        body: JSON.stringify(postdata),
        headers: { "Content-type": "application/json" },
      }).then(async (response) => {
        const responseData = await response.json();
        const customData = responseData.monitors.map((i) => ({
          title: i.friendly_name,
          uptime: i.custom_uptime_ranges,
          date: i.logs[0].datetime,
        }));
        console.log(responseData);

        setApiData(customData);
        console.log(apidata);
      });
    };
    getData();
  }, [apidata]);```
  • 1
    I think that it's what you need: https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Yumie Jul 30 '22 at 09:23
  • toLocalDateString will do the thing but I stored it in a local variable and mapped from it so I just basically don't know how to access that **date** from outside!! – shubham tarpara Jul 30 '22 at 09:37
  • @Yumie Hii, const localDate = new Date(props.date * 1000).toLocaleDateString(); i passed that date to another component and convert there thanks – shubham tarpara Aug 02 '22 at 09:53

0 Answers0