Let’s say I have a current date filtered to February 22nd 2019
. I want the nd
sign to be inside the <sup class="small"></sup>
tag. The problem is, I’m using Vue and Moment.js at the same time.
In PHP, I could just do:
{!! CarbonCarbon::today()->format('F d<sup class="small">S</sup> Y') !!}
but how can I do that in Javascript and Vue? Or maybe there is cleaner way to do so? Please take a look at my script..
JavaScript
x
30
30
1
<template>
2
<div class="container">
3
<!-- Textbox -->
4
<div class="textbox text-white">
5
<p>Realtime Flight Schedule</p>
6
<div class="live-datetime">
7
<h1>{{ currentDate | filterCurrentDate }}</h1>
8
<digi-clock></digi-clock>
9
</div>
10
</div>
11
<!-- Flight table -->
12
<flight-table></flight-table>
13
</div>
14
</template>
15
<script>
16
import moment from 'moment'
17
export default {
18
filters: {
19
filterCurrentDate(date) {
20
return moment(date).format("MMMMM Do Y")
21
}
22
},
23
data() {
24
return {
25
currentDate: new Date(),
26
}
27
},
28
}
29
</script>
30
Advertisement
Answer
I believe you have to use v-html
for that. So you’d have filterCurrentDate
generate the HTML from the Moment text, and then use v-html
to render that as-is.
JavaScript
1
2
1
<h1 v-html="filterCurrentDateHTML(currentDate)"></h1>
2
Note I changed the name of the filter to make it explicit that it returns HTML.
For filterCurrentDateHTML
, perhaps:
JavaScript
1
4
1
const m = moment(date);
2
const ord = m.format("Do");
3
return `${m.format("MMMMM")} ${ord.replace(/D/g, '')}<sup class="small">${ord.replace(/d/g, '')}</sup> ${m.format('Y')}`
4