feat: add last updated timestamp to WeatherData component

This commit is contained in:
2025-08-03 22:21:19 +02:00
parent ff77629a01
commit 38ef97b553
2 changed files with 30 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import React from "react"; import React from "react";
import { Sunrise, Sunset } from "lucide-react"; import { Sunrise, Sunset } from "lucide-react";
import { getDateTime } from "../utils/utils";
const WeatherData: React.FC = () => { const WeatherData: React.FC = () => {
const weatherRaw = localStorage.getItem("weather"); const weatherRaw = localStorage.getItem("weather");
@@ -75,6 +76,11 @@ const WeatherData: React.FC = () => {
: "--:--"} : "--:--"}
</span> </span>
</div> </div>
<div className="flex items-center gap-3">
<span className="text-base text-gray-600 italic font-semibold">
Last updated: {getDateTime()}
</span>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,24 @@
export const getDateTime = () => {
const now = new Date();
const day = now.getDate();
const month = now.toLocaleString("en-GB", { month: "long" });
const hours = now.getHours().toString().padStart(2, "0");
const minutes = now.getMinutes().toString().padStart(2, "0");
// Ordinal suffix
const getOrdinal = (n: number) => {
if (n > 3 && n < 21) return "th";
switch (n % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
};
return `${day}${getOrdinal(day)} ${month}, ${hours}:${minutes}`;
};