From 8c9b9290a651808dac24d1d2c9900c989f13ecbd Mon Sep 17 00:00:00 2001 From: GeneKung <52974083+GeneKung@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:37:01 -0800 Subject: [PATCH] Added checks for month and year --- src/components/Home/index.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/components/Home/index.js b/src/components/Home/index.js index e8f65c6..adeeed2 100644 --- a/src/components/Home/index.js +++ b/src/components/Home/index.js @@ -9,12 +9,32 @@ import config from '../../config'; const dateMatcher = /(\d{4})\-(\d{1,2})\-(\d{1,2})/; -const formatDate = dateObject => ((dateObject.getDate() === new Date().getDate() ? "Today, " : dateObject.getDate() === new Date().getDate() + 1 ? "Tomorrow, " : "") + new Intl.DateTimeFormat("en-US", { - year: "numeric", - month: "short", - day: "2-digit" - }).format(dateObject) -) +const formatDate = dateObject => { + const now = new Date(); + const isToday = dateObject.getDate() === now.getDate() && dateObject.getMonth() === now.getMonth() && dateObject.getFullYear() === now.getFullYear(); + const isTomorrow = dateObject.getDate() === now.getDate() + 1 && dateObject.getMonth() === now.getMonth() && dateObject.getFullYear() === now.getFullYear(); + + if (isToday) { + return "Today, " + new Intl.DateTimeFormat("en-US", { + year: "numeric", + month: "short", + day: "2-digit" + }).format(dateObject); + } else if (isTomorrow) { + return "Tomorrow, " + new Intl.DateTimeFormat("en-US", { + year: "numeric", + month: "short", + day: "2-digit" + }).format(dateObject); + } else { + return new Intl.DateTimeFormat("en-US", { + year: "numeric", + month: "short", + day: "2-digit" + }).format(dateObject); + } +}; + class Home extends React.Component { constructor(props) {