0.2.3 • Published 3 years ago

get-time-types v0.2.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

get-time-types

安装方式

npm install get-time-types

参数

getType:获取前后type日期('type':类型(年(yyyy)/月(MM)/日(dd)/时(HH)/分(mm)/秒(ss))

returnType:获取returnType格式的值(希望返回returnType类型的值)

            returnType可传参数:
                yyyy:返回值为年份--------------------------------------------->1970~未来任意年份        
                MM:返回值为月份-------------------------------------------->01~12
                dd:返回值为日期--------------------------------------------->01~31
                HH:返回值为小时-------------------------------------------->01~24
                mm:返回值为分钟------------------------------------------>01~60
                ss:返回值为秒-------------------------------------------->01~60
                week:返回值为星期--------------------------------------------->(日,一,二,三,四,五,六)

                
                yyyy-MM------------------------------------------------->2021-05
                MM-dd------------------------------------------------->05-11
                yyyy-MM-dd:返回值为日期(yyyy-MM-dd)---------------------------->2021-05-11

                yyyy-MM-dd HH------------------------------------------------->2021-05-11 08
                yyyy-MM-dd HH:mm------------------------------------------------->2021-05-11 08:00
                yyyy-MM-dd HH:mm:ss:返回值为时间(yyyy-MM-dd HH:mm:ss)--------------->2021-05-11 08:00:00

                MM-dd HH:mm:ss------------------------------------------------->05-11 08:00:00
                MM-dd HH:mm------------------------------------------------->05-11 08:00
                MM-dd HH------------------------------------------------->05-11 08

                dd HH:mm:ss------------------------------------------------->11 08:00:00
                dd HH:mm------------------------------------------------->11 08:00
                dd HH------------------------------------------------->11 08

                HH:mm:ss:返回值为时间(HH:mm:ss)------------------------------>08:00:00
                HH:mm----------------------------------------------->08:00
                mm:ss----------------------------------------------->00:00
                
                
            若returnType参数不为以上值或不传。则返回默认时间类型 curryDateTime(yyyy-MM-dd HH:mm:ss)


value:获取value的getType日期('value<0':前value+getType | 'value>0':后value+getType)

defaultData:默认日期(不传递该参数则默认为当前时间:接收格式为:yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss)

参数均为可选参数。参数顺序随意。若不传参数,需传递一个空对象。且得到的值为当下yyyy-MM-dd HH:mm:ss格式的时刻。

使用方法

import getTime from 'get-time-types';

var date = getTime({
    getType:"xxx",
    returnType:"xxx",
    value:"xxx",
    defaultData:"xxx"
})
The parameters are optional,if you use the default parameters. The parameter should be set to an empty object
参数是可选的,如果您想使用默认参数,则需要传递一个空的对象。

使用实例和场景

一:获取当前时间

    1.(格式为yyyy-MM-dd HH:mm:ss)

        获取当前时间参数均可不传。默认为当前时间,只需传入一个空的object,或传入returnType:"yyyy-MM-dd HH:mm:ss"
        
        例如:
            let currentTime = getTime({});

            currentTime会得到当前时刻yyyy-MM-dd HH:mm:ss格式的值

            currentTime为2021-05-11 17:00:00

            let currentTime = getTime({returnType:"yyyy-MM-dd HH:mm:ss"});

            currentTime会得到当前时刻yyyy-MM-dd HH:mm:ss格式的值

            currentTime为2021-05-11 17:00:00
    2.(格式为yyyy-MM-dd)

        希望返回的值为年月日,则需要传入returnType:"yyyy-MM-dd"
        
        例如:
            let currentTime = getTime({returnType:"yyyy-MM-dd"});

            currentTime会得到当前时刻yyyy-MM-dd格式的值

            currentTime为2021-05-11
    3.(格式为HH:mm:ss)

        希望返回的值为时分秒,则需要传入returnType:"HH:mm:ss"
        
        例如:
            let currentTime = getTime({returnType:"HH:mm:ss"});

            currentTime会得到当前时刻HH:mm:ss格式的值

            currentTime为08:00:00
    3.(只获取当前年份,月份,日期,小时,分钟,秒钟,星期)

        希望返回的值为(年份,月份,日期,小时,分钟,秒钟,星期),
        则需要传入returnType:"yyyy | MM | dd | HH | mm | ss | week"
        
        例如:
            let currentTime = getTime({returnType:"yyyy"});

            currentTime会得到当前年份

            currentTime为2021

一:获取过去/未来时间(以下例子以 2021-05-11 17:00:00 作为当前时间基准)

    1.获取过去时间,最少需要两个参数。

        getType:(过去年 | 月 | 日 | 时 | 分 | 秒);

        value:(小于0的整数。表示过去value个getType单位的时间)。

        例如:

            获取前一个月的当下时刻

            let lastMonthMoment = getTime({getType:"MM",value:-1});

            lastMonthMoment的值为:2021-04-11 17:00:00

            获取前一年的当下时刻

            let lastMonthMoment = getTime({getType:"yyyy",value:-1});

            lastMonthMoment的值为:2020-04-11 17:00:00

            获取前n天的当下时刻

            let lastMonthMoment = getTime({getType:"yyyy",value:-n});

            lastMonthMoment的值为:2021-05-11 17:00:00的n天前的此刻

    2.配合returnType参数。可获取前 n getType单位的 returnType 值
        
        例如:

            获取前一年的月份

            let lastYearCurryMonth = getTime({getType:"yyyy",value:-1,returnType:'MM'});

            lastYearCurryMonth的值为:05

            获取前一个月的日期

            let lastMonthCurryDate = getTime({getType:"MM",value:-1,returnType:'yyyy-MM-dd'});

            lastMonthCurryDate的值为:2021-04-11 17:00:00

    3.获取未来时间,与获取过去时间方式相同,只需要将 value 参数的值设为正整数即可

        例如:

            获取下个月的当前时刻

            let lastMonthCurryMoment = getTime({getType:"MM",value:1,returnType:"yyyy-MM-dd HH:mm:ss"})

            lastMonthCurryMoment的值为:2021-06-11 17:00:00

二:获取过去/未来时间,自定义基准时间

自定义基准时间需要传入一个默认的时刻,传入格式为(yyyy-MM-dd HH:mm:ss)

    例如:获取2021年1月12日 18:00:00 后三个月的时刻

    let targetTime = getTime({getType:"MM",value:3,returnType:"yyyy-MM-dd HH:mm:ss",defaultData:"2021-01-12 18:00:00"});

    targetTime的值为:2021-04-12 18:00:00