is there an easier oneliner to get 3 computed properties of current time? (current hour, minute, and second). Here's what i've got now:
const seconds = computed(() => new Date().getSeconds())
const minutes = computed(() => new Date().getMinutes())
const hours = computed(() => new Date.getHours())
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Short answer: There’s no built-in Vue trick that will magically give you three independent reactive “clock” values in a single line.
What you really need is (1) a reactive source that updates every second, and (2) three small computed props that derive hour/minute/second from that source.
Why your current code won’t work
computed() only re-runs when one of its reactive dependencies changes. new Date() is not reactive, so the code you wrote will compute once and then never update again.
A concise, idiomatic solution
VueUse already solved this problem for us with useNow() (a reactive Date that refreshes every second):
ts
import { useNow } from '@vueuse/core'
import { computed } from 'vue'
// a single reactive source that ticks every 1 000 ms
const now = useNow() // <-- reactive Date
// three tiny computeds derived from that single source
const hours = computed(() => now.value.getHours())
const minutes = computed(() => now.value.getMinutes())
const seconds = computed(() => now.value.getSeconds())
That’s readable, reactive, and only needs one extra line of setup.
(You can even squeeze it into one line if you really want: see the one-liner section below.)
Doing it without VueUse
If you don’t want an extra dependency you can DIY a ticking ref:
Why it works:
The element is 0 × 0. We give it transparent borders on three sides and a colored border on the side that should form the “tip”. The colored side collapses into a triangle.
Pros:
• One element, no pseudo-elements
• Works all the way back to IE 7
Cons:
• Harder to give it a “shaft” (the body of an arrow)
2. Arrow with a shaft (body + head) using ::after
html
<div class="arrow-right-long"></div>
css
User
Is there a way to set a border width to a percentage of a container div?
Assistant
Short version
border-width (and outline-width) accept only absolute length values (px, em, rem, vh, etc.) or the keywords thin | medium | thick.
Percentages are not allowed, so you can’t write
css
/* ❌ this is invalid CSS */
.mybox { border: 5% solid red; }
If you need a border whose thickness varies with the size of the element, you’ll have to fake it with another technique. Below are a few common work-arounds.
Work-arounds
Pseudo-element that scales with the element
html
<div class="box">Content</div>
css
.box {
position: relative;
padding: 2rem; /* space for content */
background: white;
}
.box::before { /* the “border” */
content: '';
position: absolute;
inset: 0; /* cover entire box */
border: 3vmin solid tomato; /* any scalable unit */
pointer-events: none; /* keep clicks on content */
}
Change 3vmin to any unit you like (vw, em, etc.) or to calc(10% + 0px) if you are okay with viewport-relative units.
Because percentage padding is calculated from the width of the parent box, the “border” grows and shrinks with the element.
User
about the computed date - your previous answer does not update automatically - seconds never change, i assume neither do minutes and hours
Assistant
Right — computed(() => new Date().getSeconds()) will never update, because new Date() is just a plain JS object, not a reactive dependency.
You need a reactive clock that “ticks” and then derive the pieces (h-m-s) from that.
Below are two drop-in ways to make it work.
────────────────────────────────────────
A. Easiest (VueUse) – useNow()
────────────────────────────────────────
ts
<script setup lang="ts">
import { computed } from 'vue'
import { useNow } from '@vueuse/core' // npm i @vueuse/core
const now = useNow({ interval: 1000 }) // reactive Date, refreshes every 1 s
const hours = computed(() => now.value.getHours())
const minutes = computed(() => now.value.getMinutes())
const seconds = computed(() => now.value.getSeconds())
</script>
useNow() keeps now.value fresh, so the three computeds re-run every second.
────────────────────────────────────────
B. Vanilla Vue 3 – tiny DIY ticker
────────────────────────────────────────
ts
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
const now = ref(new Date()) // 1️⃣ reactive source
// 2️⃣ make it tick
let timer: number
onMounted(() => {
timer = setInterval(() => (now.value = new Date()), 1000)
})
onUnmounted(() => clearInterval(timer))
// 3️⃣ derive the parts
const hours = computed(() => now.value.getHours())
const minutes = computed(() => now.value.getMinutes())
const seconds = computed(() => now.value.getSeconds())
</script>
────────────────────────────────────────
Why the original code can’t update
────────────────────────────────────────
computed() is lazy: it only re-evaluates when something it tracks changes.
Because new Date() is not reactive, Vue never sees a dependency change, so the function runs once and stays frozen.
────────────────────────────────────────
If you really want a “one-liner”
────────────────────────────────────────
After you have the reactive now:
Set stroke-width="5" (or any percentage in SVG 2) and the stroke scales when you scale the SVG.
Why border can’t be a percentage
CSS defines border-width as a <line-width>, and <line-width> is strictly:
• a <length> → px, em, rem, cm, …
• one of thin | medium | thick
No percentage appears in that grammar, so browsers will drop the declaration as invalid.
(Yes, border-image-widthcan take percentages, but that’s about the image slice, not the thickness of a normal border.)
Take-away
You can’t do it directly (border: 5% solid is illegal). Instead, create an element or pseudo-element whose size follows the container and make that look like a border.