mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2024-12-09 04:31:35 +08:00
feat(time): unix timestamp as time prop
This commit is contained in:
parent
3e3f4160e7
commit
a7d035b7a6
@ -1,4 +1,5 @@
|
||||
# Format
|
||||
Format time. Specs see <a href="https://date-fns.org/v2.6.0/docs/format">date-fns format</a>.
|
||||
```html
|
||||
<n-time :time="0" format="yyyy-MM-dd"/>
|
||||
<br>
|
||||
|
@ -4,4 +4,5 @@ basic
|
||||
type
|
||||
format
|
||||
relative
|
||||
unix
|
||||
```
|
20
demo/documentation/components/time/enUS/unix.md
Normal file
20
demo/documentation/components/time/enUS/unix.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Unix
|
||||
Use Unix timestamp as time.
|
||||
```html
|
||||
<n-time :time="4320000000" :to="8640000000"/>
|
||||
<br>
|
||||
<n-time :time="4320000" :to="8640000" unix/>
|
||||
<br>
|
||||
<n-time :time="4320000000" format="yyyy-MM-dd hh:mm:ss"/>
|
||||
<br>
|
||||
<n-time :time="4320000" format="yyyy-MM-dd hh:mm:ss" unix/>
|
||||
```
|
||||
```js
|
||||
export default {
|
||||
data () {
|
||||
return {
|
||||
time: new Date(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
@ -1,14 +1,12 @@
|
||||
import format from 'date-fns/format'
|
||||
import formatDistance from 'date-fns/formatDistance'
|
||||
import isValid from 'date-fns/isValid'
|
||||
import fromUnixTime from 'date-fns/fromUnixTime'
|
||||
|
||||
export default {
|
||||
name: 'NTime',
|
||||
props: {
|
||||
time: {
|
||||
validator (value) {
|
||||
return isValid(new Date(value))
|
||||
},
|
||||
type: [Number, Date],
|
||||
default: null
|
||||
},
|
||||
type: {
|
||||
@ -18,26 +16,40 @@ export default {
|
||||
default: 'relative'
|
||||
},
|
||||
to: {
|
||||
validator (value) {
|
||||
return isValid(new Date(value))
|
||||
},
|
||||
type: [Number, Date],
|
||||
default: () => new Date()
|
||||
},
|
||||
unix: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
format: {
|
||||
type: String,
|
||||
default: null
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
synthesizedTime () {
|
||||
if (this.unix) {
|
||||
return fromUnixTime(this.time)
|
||||
}
|
||||
return this.time
|
||||
},
|
||||
synthesizedTo () {
|
||||
if (this.unix) {
|
||||
return fromUnixTime(this.to)
|
||||
}
|
||||
return this.to
|
||||
},
|
||||
renderedTime () {
|
||||
if (this.format) {
|
||||
return format(this.time, this.format)
|
||||
return format(this.synthesizedTime, this.format)
|
||||
} else if (this.type === 'date') {
|
||||
return format(this.time, 'yyyy-MM-dd')
|
||||
return format(this.synthesizedTime, 'yyyy-MM-dd')
|
||||
} else if (this.type === 'datetime') {
|
||||
return format(this.time, 'yyyy-MM-dd hh:mm:ss')
|
||||
return format(this.synthesizedTime, 'yyyy-MM-dd hh:mm:ss')
|
||||
} else {
|
||||
return formatDistance(this.time, this.to, {
|
||||
return formatDistance(this.synthesizedTime, this.synthesizedTo, {
|
||||
addSuffix: true
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user