Skip to content

Commit

Permalink
fix: days & hours count
Browse files Browse the repository at this point in the history
  • Loading branch information
Valexr committed Apr 15, 2024
1 parent baab8ec commit 43d7186
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
13 changes: 9 additions & 4 deletions src/lib/components/County.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
</script>

<script lang="ts">
export let county: { years: number; months: number; days: number };
export let county: {
years: number;
months: number;
days: number;
full: Record<string, number>;
};
let type = "";
</script>
Expand All @@ -12,19 +17,19 @@
{#if type === "months"}
<li id="months">
<Button id="" bind:type>
{county.years * 12}
{county.full.months}
</Button>
</li>
{:else if type === "days"}
<li id="days">
<Button id="" bind:type>
{county.years * 12 * 4 * 7}
{county.full.days}
</Button>
</li>
{:else if type === "hours"}
<li id="hours">
<Button id="" bind:type>
{county.years * 12 * 4 * 7 * 24}
{county.full.hours}
</Button>
</li>
{:else}
Expand Down
37 changes: 21 additions & 16 deletions src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@ import { derived, readable, writable } from 'svelte/store';
import { cacheable } from './cacheable';

export const start = cacheable('startDate', '', true)
export const date = readable(new Date().toLocaleDateString("ru"))
export const time = readable(new Date().toLocaleTimeString('ru'), (set) => {
const interval = setInterval(() => {
const date = new Date().toLocaleTimeString('ru')
set(date);
}, 1000);

export const county = derived(start, ($start, set) => {
return () => clearInterval(interval);
});

export const county = derived(([start, time]), ([$start, $time], set) => {
if (start) {
const now = new Date();
const start = new Date($start);
const elapsedYears = now.getTime() - start.getTime();
const elapsedMS = now.getTime() - start.getTime();
const elapsedMonth = now.getMonth() - start.getMonth();
const elapsedDays = now.getDate() - start.getDate();
const daysInMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0,).getDate();
const years = new Date(elapsedYears).getFullYear() - 1970
const years = new Date(elapsedMS).getFullYear() - 1970
const months = (years * 12 + elapsedMonth) % 12
const days = (months * daysInMonth + elapsedDays) % daysInMonth

set({ years, months, days })
set({
years, months, days,
full: {
months: years * 12,
days: Math.trunc(elapsedMS / (3600000 * 24)),
hours: Math.trunc(elapsedMS / 3600000)
}
})
}

}, { years: 0, months: 0, days: 0 })

export const time = readable(new Date().toLocaleTimeString('ru'), (set) => {
const interval = setInterval(() => {
const date = new Date().toLocaleTimeString('ru')
set(date);
}, 1000);

return () => clearInterval(interval);
});

export const date = readable(new Date().toLocaleDateString("ru"))
}, { years: 0, months: 0, days: 0, full: {} })

export const quote = createQuote()
function createQuote() {
Expand Down

0 comments on commit 43d7186

Please sign in to comment.