diff --git a/pages/index.js b/pages/index.js index 31111a7..a6ab545 100644 --- a/pages/index.js +++ b/pages/index.js @@ -14,7 +14,7 @@ export default function Home() { body: JSON.stringify({ input, systemUsed }), }); const data = await res.json(); - console.log(data); + setWeatherData({ ...data }); setInput(""); }; @@ -39,7 +39,7 @@ export default function Home() { "E", "ESE", "SE", - "SSE", + "S/SE", "S", "SSW", "SW", @@ -61,11 +61,28 @@ export default function Home() { }; const ctoF = (c) => (c * 9) / 5 + 32; - const mpsToMph = (mps) => (mps * 2.236936).toPrecision(3); - const kmToM = (km) => (km / 1.609).toPrecision(2); + const mpsToMph = (mps) => (mps * 2.236936).toFixed(2); + const kmToM = (km) => (km / 1.609).toFixed(1); + + const timeToAMPM = (time) => { + let hours = time.split(":")[0]; + let minutes = time.split(":")[1]; + hours = hours % 12; + hours = hours ? hours : 12; // the hour '0' should be '12' + // minutes = minutes < 10 ? "0" + minutes : minutes; + return hours + ":" + minutes; + }; + + const isPM = (time) => { + let hours = time.split(":")[0]; + if (hours >= 12) { + return "PM"; + } else { + return "AM"; + } + }; const changeSystem = () => { - console.log("system changed"); if (systemUsed == "metric") { setSystemUsed("imperial"); } else { @@ -128,18 +145,23 @@ export default function Home() { ] } ,{" "} - { - convertTime(weatherData.dt, weatherData.timezone)[0].split( - ":" - )[0] - } - :00 + {systemUsed == "metric" + ? convertTime(weatherData.dt, weatherData.timezone)[0].split( + ":" + )[0] + : timeToAMPM( + convertTime(weatherData.dt, weatherData.timezone)[0] + ).split(":")[0]} + :00{" "} + {systemUsed == "imperial" + ? isPM(convertTime(weatherData.dt, weatherData.timezone)[0]) + : ""} )} (e.target.value = "")} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => something(e)} @@ -226,14 +248,28 @@ export default function Home() { />
AM
++ {systemUsed == "imperial" + ? isPM( + convertTime( + weatherData.sys.sunrise, + weatherData.timezone + )[0] + ) + : ""} +
PM
++ {systemUsed == "imperial" + ? isPM( + convertTime( + weatherData.sys.sunset, + weatherData.timezone + )[0] + ) + : ""} +