From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_BLOCKED, URIBL_DBL_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1696301548; bh=b2WXWNUug02lfyUAB5q05gA8vcuUKh1ViuX4FWTpbNc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Eq5wQz78cYQDxNBHZwADFhf6f5g+DtClYL2bil3juMwuM9eLxNPYQAaqa4/Mfkrxa p4ckFM2yTSa1rm72wKPeZtiFDakPDIX7gBd4aIfLrpnppBhLdO9hbNo14geKjN/q7o dAV9X9sTzb3r93TATjjt+F6U3uUABXds25iPUAkZ86TVCKAoayw4HoRM1Hh1FIaDrC rfzXs4OMFXnJpucC+eZOVtfnm6luzQpvclb5wZE7rcmLH18xgbY1n/MatzBM0Vmp7M xg+H3vptG76I2cPsZi7bhLrRIUB6tdZiAkgbw877Y0HkvzDzddgp60TtJ2s9i64t89 jCoOg6XPkMQxA== Received: from localhost.localdomain (unknown [175.158.50.50]) by gnuweeb.org (Postfix) with ESMTPSA id 13B4924B8C8; Tue, 3 Oct 2023 09:52:26 +0700 (WIB) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , Irvan Malik Azantha , Memet Zx , GNU/Weeb Mailing List Subject: [PATCH v1 07/13] feat(lib/functions): add dateFormat() function Date: Tue, 3 Oct 2023 09:51:40 +0700 Message-Id: <20231003025146.1557-8-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20231003025146.1557-1-kiizuha@gnuweeb.org> References: <20231003025146.1557-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This commit introduces the dateFormat() function, which is used to format datetime string returned by the API response. The formatted date appears as follows: "Today at 09:00", "Yesterday at 09:00", "Oct 02 at 09:00", and so on. Signed-off-by: Muhammad Rizki --- src/lib/functions.ts | 29 +++++++++++++++++++++++++++++ src/lib/index.ts | 6 ++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/lib/functions.ts b/src/lib/functions.ts index 510f13e..2928824 100644 --- a/src/lib/functions.ts +++ b/src/lib/functions.ts @@ -102,3 +102,32 @@ export const getRepliedMessage = ( return msg.message_id === current.reply_to_message_id }); } + +export const dateFormat = (date: string, amPm: boolean = false): string => { + const ts = new Date(date); + const today = new Date(); + + const tsDate = ts.getDate(); + const todayDate = today.getDate(); + + const tsMonth = ts.getMonth(); + const todayMonth = today.getMonth(); + + const tsYear = ts.getFullYear(); + const todayYear = today.getFullYear(); + + const calc = todayDate - tsDate; + const localeTime = ts.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: amPm }); + + if (tsDate === todayDate &&tsMonth === todayMonth && tsYear === todayYear) { + return `Today at ${localeTime}`; + } else if (calc === 1 && tsMonth === todayMonth && tsYear === todayYear) { + return `Yesterday at ${localeTime}`; + } else if (calc === -1 && tsMonth === todayMonth && tsYear === todayYear) { + return `Tomorrow at ${localeTime}`; + } else if (tsYear === todayYear) { + return `${tsDate} ${ts.toLocaleString('en-US', { month: 'short' })} at ${localeTime}`; + } else { + return `${ts.toLocaleString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })} at ${localeTime}`; + } +} diff --git a/src/lib/index.ts b/src/lib/index.ts index b62f0bd..2c71df5 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -9,7 +9,8 @@ import { getFixedRandomColor, cleanMessageText, setupUserName, - getRepliedMessage + getRepliedMessage, + dateFormat } from "./functions"; export { @@ -20,5 +21,6 @@ export { getFixedRandomColor, cleanMessageText, setupUserName, - getRepliedMessage + getRepliedMessage, + dateFormat }; -- Muhammad Rizki