public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Muhammad Rizki <kiizuha@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Muhammad Rizki <kiizuha@gnuweeb.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
	Irvan Malik Azantha <irvanma@gnuweeb.org>,
	Memet Zx <zxce3@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH v2 08/14] feat(lib/functions): add dateFormat() function
Date: Sat,  7 Oct 2023 12:44:40 +0700	[thread overview]
Message-ID: <20231007054446.1204-9-kiizuha@gnuweeb.org> (raw)
In-Reply-To: <20231007054446.1204-1-kiizuha@gnuweeb.org>

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 <kiizuha@gnuweeb.org>
---
 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


  parent reply	other threads:[~2023-10-07  5:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-07  5:44 [PATCH v2 00/14] GNU/Weeb Web Migration to SvelteKit Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 01/14] refactor: migrate: migrate to sveltekit framework Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 02/14] feat(layout): add +layout.ts Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 03/14] feat(app): add app types file Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 04/14] feat(lib): add API_URL and STORAGE_URL constant variable Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 05/14] feat(lib): add lib functions file Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 06/14] feat(lib): add newly created lib constants variables and functions Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 07/14] feat(lib/functions): add getRepliedMessage() function Muhammad Rizki
2023-10-07  5:44 ` Muhammad Rizki [this message]
2023-10-07  5:44 ` [PATCH v2 09/14] feat(components): add <OrganizationMembers/> component Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 10/14] feat(components): add <RecentMessages/> component Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 11/14] feat(routes): add +page.ts Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 12/14] feat(routes/page): add +page.svelte Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 13/14] chore(README): update README.md Muhammad Rizki
2023-10-07  5:44 ` [PATCH v2 14/14] chore: remove unused files Muhammad Rizki
2023-10-09 21:22 ` [PATCH v2 00/14] GNU/Weeb Web Migration to SvelteKit Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231007054446.1204-9-kiizuha@gnuweeb.org \
    --to=kiizuha@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    --cc=irvanma@gnuweeb.org \
    --cc=zxce3@gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox