Fix formatting invalid dates

This commit is contained in:
kolaente 2021-04-22 14:26:48 +02:00
parent 8d04bdc4f0
commit 12b8d47a79
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 34 additions and 27 deletions

View file

@ -157,7 +157,7 @@ export default {
} }
}, },
mounted() { mounted() {
this.date = this.value this.setDateValue(this.value)
document.addEventListener('click', this.hideDatePopup) document.addEventListener('click', this.hideDatePopup)
}, },
beforeDestroy() { beforeDestroy() {
@ -165,11 +165,7 @@ export default {
}, },
watch: { watch: {
value(newVal) { value(newVal) {
if(newVal === null) { this.setDateValue(newVal)
this.date = null
return
}
this.date = createDateFromString(newVal)
}, },
flatPickrDate(newVal) { flatPickrDate(newVal) {
this.date = createDateFromString(newVal) this.date = createDateFromString(newVal)
@ -177,6 +173,13 @@ export default {
}, },
}, },
methods: { methods: {
setDateValue(newVal) {
if(newVal === null) {
this.date = null
return
}
this.date = createDateFromString(newVal)
},
updateData() { updateData() {
this.changed = true this.changed = true
this.$emit('input', this.date) this.$emit('input', this.date)

View file

@ -7,6 +7,10 @@
* @returns {Date} * @returns {Date}
*/ */
export const createDateFromString = dateString => { export const createDateFromString = dateString => {
if (dateString instanceof Date) {
return dateString
}
if (dateString.includes('-')) { if (dateString.includes('-')) {
dateString = dateString.replace(/-/g, "/") dateString = dateString.replace(/-/g, "/")
} }

View file

@ -2,7 +2,9 @@ import Vue from 'vue'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
import {createDateFromString} from '@/helpers/time/createDateFromString'
import {VERSION} from './version.json' import {VERSION} from './version.json'
// Register the modal // Register the modal
import Modal from './components/modal/modal' import Modal from './components/modal/modal'
// Add CSS // Add CSS
@ -166,10 +168,21 @@ Vue.directive('focus', focus)
import tooltip from '@/directives/tooltip' import tooltip from '@/directives/tooltip'
Vue.directive('tooltip', tooltip) Vue.directive('tooltip', tooltip)
const formatDate = (date, f) => { const dateIsValid = date => {
if (typeof date === 'string') { if (date === null) {
date = new Date(date) return false
} }
return date instanceof Date && !isNaN(date)
}
const formatDate = (date, f) => {
if (!dateIsValid(date)) {
return ''
}
date = createDateFromString(date)
return date ? format(date, f) : '' return date ? format(date, f) : ''
} }
@ -182,13 +195,12 @@ Vue.component('card', Card)
Vue.mixin({ Vue.mixin({
methods: { methods: {
formatDateSince: date => { formatDateSince: date => {
if (date === null) { if (!dateIsValid(date)) {
return '' return ''
} }
if (typeof date === 'string') { date = createDateFromString(date)
date = new Date(date)
}
const currentDate = new Date() const currentDate = new Date()
let formatted = '' let formatted = ''
if (date > currentDate) { if (date > currentDate) {
@ -201,20 +213,8 @@ Vue.mixin({
return formatted return formatted
}, },
formatDate: date => { formatDate: date => formatDate(date, 'PPPPpppp'),
if (date === null) { formatDateShort: date => formatDate(date, 'PPpp'),
return ''
}
if (typeof date === 'string') {
date = new Date(date)
}
return date ? format(date, 'PPPPpppp') : ''
},
formatDateShort: date => {
console.log('short date', date)
return formatDate(date, 'PPpp')
},
error: (e, context, actions = []) => message.error(e, context, actions), error: (e, context, actions = []) => message.error(e, context, actions),
success: (s, context, actions = []) => message.success(s, context, actions), success: (s, context, actions = []) => message.success(s, context, actions),
colorIsDark: colorIsDark, colorIsDark: colorIsDark,