Skip to content

Commit c2e1200

Browse files
pdanpdanrstoenescu
authored andcommitted
close quasarframework#1204: Allow custom text inside brackets in formatDate (quasarframework#1212)
* Allow custom text inside brackets in formatDate * Add sample for date format and fix extra spaces in QInput * Update date.js
1 parent f45989f commit c2e1200

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

dev/components/form/datetime.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
</span>
1313
</p>
1414

15-
<div class="label bg-secondary text-white">
16-
Model <span class="right-detail"><em>{{model}}</em></span>
15+
<div class="bg-secondary text-white">
16+
Model: <em>{{model}}</em>
17+
</div>
18+
<q-input v-model="format" float-label="Format string" />
19+
<div class="bg-secondary text-white">
20+
Formatted: <em>{{modelFormatted}}</em>
1721
</div>
1822

1923
<p class="caption">
@@ -179,12 +183,19 @@ export default {
179183
model: undefined,
180184
defaultSelection: '2016-09-18T10:45:00.000Z',
181185
186+
format: 'MMMM D, YYYY [at] h:mm [[]a[\\]]',
187+
182188
minMaxModel: date.formatDate(day),
183189
184190
min: date.subtractFromDate(day, {days: 5}),
185191
max: date.addToDate(day, {days: 4, month: 1, minutes: 10})
186192
}
187193
},
194+
computed: {
195+
modelFormatted () {
196+
return date.formatDate(this.model, this.format)
197+
}
198+
},
188199
methods: {
189200
onChange (val) {
190201
console.log('@change', val)

src/components/input/QInput.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export default {
250250
251251
__set (e) {
252252
let val = e.target ? e.target.value : e
253-
253+
254254
if (this.isNumber) {
255255
val = parseFloat(val)
256256
if (isNaN(val)) {
@@ -264,7 +264,7 @@ export default {
264264
else if (this.upperCase) {
265265
val = val.toUpperCase()
266266
}
267-
267+
268268
if (val !== this.model) {
269269
this.$emit('input', val)
270270
}

src/utils/date.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const
88
MILLISECONDS_IN_DAY = 86400000,
99
MILLISECONDS_IN_HOUR = 3600000,
1010
MILLISECONDS_IN_MINUTE = 60000,
11-
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
11+
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
1212

1313
function formatTimezone (offset, delimeter = '') {
1414
const
@@ -507,10 +507,12 @@ export function formatDate (val, mask = 'YYYY-MM-DDTHH:mm:ss.SSSZ') {
507507

508508
let date = new Date(val)
509509

510-
return mask.replace(token, function (match) {
510+
return mask.replace(token, function (match, text) {
511511
if (match in formatter) {
512512
return formatter[match](date)
513513
}
514-
return match
514+
return text === void 0
515+
? match
516+
: text.split('\\]').join(']')
515517
})
516518
}

0 commit comments

Comments
 (0)