10 changed files with 176 additions and 123 deletions
@ -0,0 +1,16 @@
@@ -0,0 +1,16 @@
|
||||
<script> |
||||
import { display, to_secs } from '../time.svelte.js'; |
||||
let { timestamp = $bindable() } = $props(); |
||||
|
||||
let datetime = $state(null); |
||||
|
||||
$effect(() => { |
||||
datetime = display(timestamp); |
||||
}); |
||||
|
||||
$effect(() => { |
||||
timestamp = to_secs(datetime); |
||||
}); |
||||
</script> |
||||
|
||||
<input type="datetime-local" bind:value={datetime} /> |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
export function display(timestamp){ |
||||
const date = new Date(timestamp * 1000); |
||||
const year = date.getFullYear(); |
||||
const month = String(date.getMonth() + 1).padStart(2, '0'); |
||||
const day = String(date.getDate()).padStart(2, '0'); |
||||
const hours = String(date.getHours()).padStart(2, '0'); |
||||
const minutes = String(date.getMinutes()).padStart(2, '0'); |
||||
return `${year}-${month}-${day} ${hours}:${minutes}`; |
||||
} |
||||
|
||||
function padTo2Digits(num) { |
||||
return num.toString().padStart(2, '0'); |
||||
} |
||||
|
||||
export function msToTime(ms) { |
||||
const totalSeconds = Math.floor(ms / 1000); |
||||
const days = Math.floor(totalSeconds / 86400); |
||||
const hours = Math.floor((totalSeconds % 86400) / 3600); |
||||
const minutes = Math.floor((totalSeconds % 3600) / 60); |
||||
const seconds = totalSeconds % 60; |
||||
|
||||
let timestring = ''; |
||||
if (days) timestring += days.toString().padStart(2, '0') + ":"; |
||||
if (days||hours) timestring += hours.toString().padStart(2, '0') + ":"; |
||||
return timestring + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0'); |
||||
} |
||||
|
||||
export function now(){ |
||||
return Math.floor(Date.now()/1000); |
||||
} |
||||
|
||||
export function to_secs(datetime){ |
||||
return Math.floor(new Date(datetime).getTime()/1000); |
||||
} |
||||
Loading…
Reference in new issue