Guest User

Untitled

a guest
Feb 1st, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 4.44 KB | Source Code | 0 0
  1. import { createSignal } from "solid-js";
  2. import { Button } from "./Button";
  3. import { Card } from "./Card";
  4.  
  5. function daysInMonth(date: Date): number {
  6.     const month = date.getMonth();
  7.     const year = date.getFullYear();
  8.    
  9.     if (month === 1) {
  10.         // February
  11.         if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
  12.             return 29; // Leap year
  13.         } else {
  14.             return 28;
  15.         }
  16.     }
  17.     else {
  18.         return [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
  19.     }
  20. }
  21.  
  22. function monthName(date: Date): string {
  23.     const month = date.getMonth();
  24.     return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][month]; //TODO: Localization
  25. }
  26.  
  27. interface DateSelectorProps {
  28.  
  29. }
  30.  
  31. export function DateSelector() {
  32.     const [selectedDate, setSelectedDate] = createSignal(new Date(9999, 0, 1));
  33.     const [disableYearUp, setDisableYearUp] = createSignal(selectedDate().getFullYear() >= 9999);
  34.     const [disableYearDown, setDisableYearDown] = createSignal(selectedDate().getFullYear() <= 1500);
  35.  
  36.     const updateDate = (yearOffset: number, monthOffset: number, dayOffset: number) => {
  37.         const current = selectedDate();
  38.        
  39.         let newYear = current.getFullYear() + yearOffset;
  40.         let newMonth = current.getMonth() + monthOffset;
  41.         let newDay = current.getDate() + dayOffset;
  42.  
  43.         if (newYear < 1500) newYear = 1500;
  44.         if (newYear > 9999) newYear = 9999;
  45.  
  46.         if (newMonth < 0) {
  47.             newMonth = 11;
  48.         }
  49.         if (newMonth > 11) {
  50.             newMonth = 0;
  51.         }
  52.  
  53.         const nextDate = new Date(newYear, newMonth, newDay);
  54.  
  55.         let daysInNextMonth = daysInMonth(new Date(newYear, newMonth, 1));
  56.         if (nextDate.getMonth() !== newMonth) {
  57.             nextDate.setDate(daysInNextMonth);
  58.         }
  59.  
  60.         setSelectedDate(nextDate);
  61.  
  62.         if (nextDate.getFullYear() === 9999) {
  63.             setDisableYearUp(true);
  64.         } else {
  65.             setDisableYearUp(false);
  66.         }
  67.  
  68.         if (nextDate.getFullYear() === 1500) {
  69.             setDisableYearDown(true);
  70.         } else {
  71.             setDisableYearDown(false);
  72.         }
  73.     };
  74.  
  75.     return (
  76.         <Card class=""
  77.             body={
  78.                 <div class="flex flex-row justify-center items-center space-x-2">
  79.                     <div class="flex flex-col items-center justify-center border-x border-black rounded-2xl">
  80.                         <Button class="w-full" onclick={() => updateDate(0, 1, 0)}>
  81.                             <p class="text-2xl">Up</p>
  82.                         </Button>
  83.                         <div class="">
  84.                             <p class="text-3xl text-center">{monthName(selectedDate())}</p>
  85.                         </div>
  86.                         <Button class="w-full px-12" onclick={() => updateDate(0, -1, 0)}>
  87.                             <p class="text-2xl">Down</p>
  88.                         </Button>
  89.                     </div>
  90.                     <div class="flex flex-col items-center justify-center border-x border-black rounded-2xl">
  91.                         <Button class="w-full" onclick={() => updateDate(0, 0, 1)}>
  92.                             <p class="text-2xl">Up</p>
  93.                         </Button>
  94.                         <div class="">
  95.                             <p class="text-3xl text-center">{selectedDate().getDate()}</p>
  96.                         </div>
  97.                         <Button class="w-full" onclick={() => updateDate(0, 0, -1)}>
  98.                             <p class="text-2xl">Down</p>
  99.                         </Button>
  100.                     </div>
  101.                     <div class="flex flex-col items-center justify-center border-x border-black rounded-2xl">
  102.                         <Button class="w-full" onclick={() => updateDate(1, 0, 0)} disabled={disableYearUp()}>
  103.                             <p class="text-2xl">Up</p>
  104.                         </Button>
  105.                         <div class="">
  106.                             <p class="text-3xl text-center">{selectedDate().getFullYear()}</p>
  107.                         </div>
  108.                         <Button class="w-full" onclick={() => updateDate(-1, 0, 0)} disabled={disableYearDown()}>
  109.                             <p class="text-2xl">Down</p>
  110.                         </Button>
  111.                     </div>
  112.                 </div>
  113.             }
  114.         />
  115.     );
  116. }
Advertisement
Add Comment
Please, Sign In to add comment