Skip to content

Commit cc93fb8

Browse files
committed
feat: New date format Do, Qo (adds st, nd, rd, th suffix)
1 parent 54b66d3 commit cc93fb8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

dev/components/form/datetime.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</span>
3535
</small>
3636
</p>
37-
<q-datetime format="YYYY-MMMM-dddd" v-model="model" type="date" align="right" />
37+
<q-datetime format="YYYY-MMMM-dddd Do Qo Q" v-model="model" type="date" align="right" />
3838
<q-datetime stack-label="Stack Label" v-model="model" type="date" />
3939
<q-datetime float-label="Float Label" v-model="model" type="date" />
4040

src/utils/date.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const
77
MILLISECONDS_IN_DAY = 86400000,
88
MILLISECONDS_IN_HOUR = 3600000,
99
MILLISECONDS_IN_MINUTE = 60000,
10-
token = /d{1,4}|M{1,4}|m{1,2}|w{1,2}|D{1,4}|YY(?:YY)?|H{1,2}|h{1,2}|s{1,2}|S{1,3}|Z{1,2}|a{1,2}|[AQExX]/g
10+
token = /d{1,4}|M{1,4}|m{1,2}|w{1,2}|Qo|Do|D{1,4}|YY(?:YY)?|H{1,2}|h{1,2}|s{1,2}|S{1,3}|Z{1,2}|a{1,2}|[AQExX]/g
1111

1212
export const dayNames = [
1313
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
@@ -297,6 +297,18 @@ export function daysInMonth (date) {
297297
return (new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate()
298298
}
299299

300+
function getOrdinal (n) {
301+
if (n >= 11 && n <= 13) {
302+
return `${n}th`
303+
}
304+
switch (n % 10) {
305+
case 1: return `${n}st`
306+
case 2: return `${n}nd`
307+
case 3: return `${n}rd`
308+
}
309+
return `${n}th`
310+
}
311+
300312
export const formatter = {
301313
// Year: 00, 01, ..., 99
302314
YY (date) {
@@ -333,11 +345,21 @@ export const formatter = {
333345
return Math.ceil((date.getMonth() + 1) / 3)
334346
},
335347

348+
// Quarter: 1st, 2nd, 3rd, 4th
349+
Qo (date) {
350+
return getOrdinal(this.Q(date))
351+
},
352+
336353
// Day of month: 1, 2, ..., 31
337354
D (date) {
338355
return date.getDate()
339356
},
340357

358+
// Day of month: 1st, 2nd, ..., 31st
359+
Do (date) {
360+
return getOrdinal(date.getDate())
361+
},
362+
341363
// Day of month: 01, 02, ..., 31
342364
DD (date) {
343365
return pad(date.getDate())

0 commit comments

Comments
 (0)