fix(datetime): local datetime parsing
This commit is contained in:
@@ -3,6 +3,7 @@ import { Eye, EyeOff, Copy, Check } from "lucide-react";
|
|||||||
import Box from "../components/Box.tsx";
|
import Box from "../components/Box.tsx";
|
||||||
import { getUser } from "../utils/endpoints.ts";
|
import { getUser } from "../utils/endpoints.ts";
|
||||||
import type { User } from "../utils/types.ts";
|
import type { User } from "../utils/types.ts";
|
||||||
|
import { formatLocalDate } from "../utils/utils.ts";
|
||||||
|
|
||||||
const Profile = () => {
|
const Profile = () => {
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<User | null>(null);
|
||||||
@@ -44,7 +45,7 @@ const Profile = () => {
|
|||||||
<p className="text-sm text-gray-500">@{user.username}</p>
|
<p className="text-sm text-gray-500">@{user.username}</p>
|
||||||
<p className="text-sm text-gray-500">{user.email}</p>
|
<p className="text-sm text-gray-500">{user.email}</p>
|
||||||
<p className="text-xs text-gray-400 mt-1">
|
<p className="text-xs text-gray-400 mt-1">
|
||||||
Member since {new Date(user.createdAt).toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric" })}
|
Member since {formatLocalDate(user.createdAt)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -7,13 +7,20 @@ function formatTime(seconds: number): string {
|
|||||||
return `${m}:${pad(s)}`;
|
return `${m}:${pad(s)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parses a UTC date string, appending Z if no timezone offset is present so
|
||||||
|
// the browser always interprets the value as UTC rather than local time.
|
||||||
function stringToDate(dateString: string): Date {
|
function stringToDate(dateString: string): Date {
|
||||||
const normalized = dateString.replace(/(\.\d{3})\d+/, "$1");
|
const normalized = dateString.replace(/(\.\d{3})\d+/, "$1");
|
||||||
const date = new Date(normalized);
|
const withTz = /[Zz]$|[+-]\d{2}:\d{2}$|[+-]\d{4}$/.test(normalized) ? normalized : `${normalized}Z`;
|
||||||
|
const date = new Date(withTz);
|
||||||
if (isNaN(date.getTime())) throw new Error("Invalid date string");
|
if (isNaN(date.getTime())) throw new Error("Invalid date string");
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatLocalDate(dateString: string, options?: Intl.DateTimeFormatOptions): string {
|
||||||
|
return stringToDate(dateString).toLocaleDateString(undefined, options ?? { year: "numeric", month: "long", day: "numeric" });
|
||||||
|
}
|
||||||
|
|
||||||
function dateToTimeAgo(date: Date): string {
|
function dateToTimeAgo(date: Date): string {
|
||||||
const secondsAgo = Math.floor((Date.now() - date.getTime()) / 1000);
|
const secondsAgo = Math.floor((Date.now() - date.getTime()) / 1000);
|
||||||
const units: [number, string][] = [
|
const units: [number, string][] = [
|
||||||
@@ -33,4 +40,4 @@ function dateToTimeAgo(date: Date): string {
|
|||||||
return `${s} second${s !== 1 ? 's' : ''} ago`;
|
return `${s} second${s !== 1 ? 's' : ''} ago`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { formatTime, stringToDate, dateToTimeAgo };
|
export { formatTime, stringToDate, dateToTimeAgo, formatLocalDate };
|
||||||
|
|||||||
Reference in New Issue
Block a user