Skip to content
On this page

useDate

操作时间,返回期望格式(内部使用了 dayjs)

Demo

2023-02-05 00:18:16

Usage

html
<template>
  <div   >
    <p>{{ data }}</p>
    <button @click="refresh()">刷新时间</button>
  </div>
</template>

<script lang="ts" setup>
import { useDate } from '@morehook/core'

const { data, refresh } = useDate({
  method: 'hour',
  methodParam: 0
})
</script>

Type Declarations

typescript
type Value = string | number | Date
/**
 * format: 针对日期格式化(默认 YYYY-MM-DD HH:mm:ss)
 * method: 获取时间的操作方法(默认 format)
 * methodParam: 针对获取到的时间 (例如 dayjs().hour(methodParam = 10) 就是将获取到的时间中小时时间设置为10)
 * 注意:比如说 method 设为 hour,methodParam 不设置时,dayjs返回的是当前小时数(比如当前是10点则返回10)
 */
interface Options {
  format?: string
  method?:
    | "format"
    | "timestamp"
    | "millisecond"
    | "second"
    | "minute"
    | "hour"
    | "date"
    | "day"
    | "month"
    | "year"
  methodParam?: number
}
/**
 * 操作时间(内部使用了 dayjs)
 * @param options
 * @param initialValue 初始时间
 */
export declare function useDate(
  options?: Options,
  initialValue?: Value | undefined
): {
  readonly data: any
  refresh: (refreshValue?: Value) => void
}

Source

SourceDemoDocs