v3hooks --useDate

import { ref, readonly } from 'vue';
import dayjs from 'dayjs';


const defaultOptions ={
    format: 'YYYY-MM-DD HH:mm:ss',
    method: 'format'
};

type Value =  string | number | Date;

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: (refreshValue?: Value) => void;
}

function useDate( initialValue?:Value, options?: Options ){
    
    const state = ref<string>();
    
    let value = initialValue || +new Date();
    const {
        format,
        method,
        methodParam
    } = { ...defaultOptions, ...options }


    const refresh = ( refreshValue?:Value )=>{
        console.log(refreshValue);
        value = refreshValue || +new Date();
        switch( method ){
            case 'format':
                state.value = dayjs(value).format(format);
                break;
            case undefined :
                break;
            default:
                let data: any = dayjs(value);
                if( methodParam ){
                    data = data[method](methodParam);
                    if( options && options.format ){
                        data = data.format(format);
                    }
                }
                state.value = data
        }
       
    };

    refresh();

    const data = readonly( state);

    return {
        data,
        refresh
    }
};


export default useDate

useDate

一个用来操作时间的 Hook 。

内部使用了 dayjs 作为format工具

使用Demo

<template>
  <div class="hello">
    <div> value:{{ data }}</div>
    <button @click="handleUpdateTime">无参数刷新</button>
    <button @click="handleUpdateTimeParam">有参数刷新</button>
  </div>
</template>

<script lang="ts">
import { useDate } from "v3hooks";

export default {
  
  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>

useDate接受一个时间并根据options来返回格式化后的数据。 可根据返回的refresh来进行更新调用.

其中method的参数具体使用可以参考dayjs的 取值/赋值

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值