refactor: move some date things about and separate, remove unnecessary files

This commit is contained in:
David Ralph
2024-09-20 23:30:07 +01:00
parent 9393c20ea8
commit 728ef587e1
9 changed files with 45 additions and 61 deletions

View File

@@ -1,7 +1,7 @@
import variables from 'config/variables';
import { useRef, useState, useEffect } from 'react';
import { nth, convertTimezone } from 'utils/date';
import { appendNth, convertTimezone } from 'utils/date';
import EventBus from 'utils/eventbus';
import defaults from './options/default';
@@ -107,7 +107,7 @@ function Greeting() {
if (birth.getDate() === now.getDate() && birth.getMonth() === now.getMonth()) {
if (localStorage.getItem('birthdayage') === 'true' && calculateAge(birth) !== 0) {
const text = variables.getMessage('widgets.greeting.birthday').split(' ');
message = `${text[0]} ${nth(calculateAge(birth))} ${text[1]}`;
message = `${text[0]} ${appendNth(calculateAge(birth))} ${text[1]}`;
} else {
message = variables.getMessage('widgets.greeting.birthday');
}

View File

@@ -1,7 +1,7 @@
import variables from 'config/variables';
import { useState, useEffect, useRef, useCallback } from 'react';
import { nth, convertTimezone } from 'utils/date';
import { appendNth, convertTimezone } from 'utils/date';
import EventBus from 'utils/eventbus';
import defaults from './options/default';
@@ -98,7 +98,7 @@ const DateWidget = () => {
const datenth =
localStorage.getItem('datenth') === 'true'
? nth(currentDate.getDate())
? appendNth(currentDate.getDate())
: currentDate.getDate();
const dateDay =

View File

View File

@@ -1,19 +0,0 @@
.mobile {
.navbar {
flex-flow: column;
}
.searchComponents {
display: none;
}
#modal {
// fill screen
width: 100vw;
height: 100vh;
}
.modalSidebar {
display: none;
}
}

View File

@@ -1,5 +1,6 @@
@import 'variables';
@import 'toast';
@tailwind base;
@tailwind components;
@tailwind utilities;

View File

@@ -0,0 +1,13 @@
/**
* It takes a date and a timezone and returns a new date object with the timezone applied.
* @param date - The date you want to convert.
* @param tz - The timezone you want to convert to.
* @returns A new Date object with the timezone set to the timezone passed in.
*/
export function convertTimezone(date, tz) {
return new Date(
(typeof date === 'string' ? new Date(date) : date).toLocaleString('en-US', {
timeZone: tz,
}),
);
}

22
src/utils/date/getNth.js Normal file
View File

@@ -0,0 +1,22 @@
/**
* If the number is between 3 and 20, return the number with the suffix "th". Otherwise, return the
* number with the suffix "st", "nd", "rd", or "th" depending on the last digit of the number
* @param d - The day of the month.
* @returns the day of the month with the appropriate suffix.
*/
export function appendNth(d) {
if (d > 3 && d < 21) {
return d + 'th';
}
switch (d % 10) {
case 1:
return d + 'st';
case 2:
return d + 'nd';
case 3:
return d + 'rd';
default:
return d + 'th';
}
}

View File

@@ -1,37 +1,4 @@
// todo: maybe move stuff
/**
* If the number is between 3 and 20, return the number with the suffix "th". Otherwise, return the
* number with the suffix "st", "nd", "rd", or "th" depending on the last digit of the number
* @param d - The day of the month.
* @returns the day of the month with the appropriate suffix.
*/
export function nth(d) {
if (d > 3 && d < 21) {
return d + 'th';
}
import { convertTimezone } from './convertTimezone';
import { getNth } from './getNth';
switch (d % 10) {
case 1:
return d + 'st';
case 2:
return d + 'nd';
case 3:
return d + 'rd';
default:
return d + 'th';
}
}
/**
* It takes a date and a timezone and returns a new date object with the timezone applied.
* @param date - The date you want to convert.
* @param tz - The timezone you want to convert to.
* @returns A new Date object with the timezone set to the timezone passed in.
*/
export function convertTimezone(date, tz) {
return new Date(
(typeof date === 'string' ? new Date(date) : date).toLocaleString('en-US', {
timeZone: tz,
}),
);
}
export { convertTimezone, getNth };