useDate

一个用来操作时间的 Hook。

基础用法

value:2022-08-17 03:29:56
<template>
  <div class="hello">
    <div> value:{{ data }}</div>
    <el-button @click="handleUpdateTime">无参数刷新</el-button>
    <el-button @click="handleUpdateTimeParam">有参数刷新</el-button>
  </div>
</template>

<script lang="ts">
import { useDate } from "dz-hooks";

export default {
  props: {
    msg: String,
  },
  setup() {
    const { data, refresh } = useDate(+new Date(), {
      format: 'YYYY-MM-DD HH:mm:ss',
      method: 'hour',
      methodParam: 3
    });

    const handleUpdateTime = () => {
      refresh();
    }

    const handleUpdateTimeParam = () => {
      refresh('2021-7-16 12:17:00');
    }

    return {
      data,
      refresh,
      handleUpdateTime,
      handleUpdateTimeParam
    };
  },
};
</script>

Api

interface Options{
    format?: string
    method?: 'format' | 'millisecond' | 'second' | 'minute' | 'hour' | 'date' |'day'  | 'month' | 'year',
    methodParam?: number 
}

function useDate(
    value?: Value | undefined, 
    options?: Options
): {
    readonly data: any,
    refresh: () => void;
}

Params

参数说明类型默认值
initialValue初始化的时间值string - number - DateDate
options可选项,配置时间属性,详见 OptionsOptions-

Options

参数说明类型默认值
format针对日期格式化stringYYYY-MM-DD HH:mm:ss
method获取时间的操作方法见Api Options.methodformat
methodParam针对日期格式化的操作方法的参数number-

Result

参数说明类型默认值
data格式化后的时间值Ref-
refresh格式化后的时间值(refreshValue)=> void-
Last Updated:
Contributors: huzhiwei