YuKi
论坛版主
论坛版主
  • 最后登录2024-03-25
  • 发帖数20
  • 社区居民
阅读:5364回复:0

(记录)js的一些方法,不定时更新

楼主#
更多 发布于:2021-03-12 14:11
2021-3-12更新
时间操作类:


1.计算n天后的日期
/**
 * @method fun_date 计算n天后的日期
 * @param {int} num 可以为负数,从当天计算。
 * @return {string} n天后的日期,格式为YYYY-mm-dd
 * */
function fun_date(num) {
    var date1 = new Date();
    var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() + num);
    var m;
    var d;
    if(date2.getMonth() + 1 < 10) m="0"+ (date2.getMonth() + 1);
    else m = date2.getMonth() + 1;
    if(date2.getDate()<10) d = "0"+date2.getDate();
    else d = date2.getDate();
    var time2 = date2.getFullYear() + "-" + m + "-" + d;
    return time2;
}

2.计算两个日期内所有日期
/**
 * @method getdiffdate 计算两个日期内所有日期,包含这两个日期
 * @param {string} stime 开始时间  日期格式:YYYY-MM-DD
 * @param {string} etime 结束时间  日期格式:YYYY-MM-DD
 * @return {Array} 在stime和etime之中的所有日期的数组,包含stime和etime
 * */
function getdiffdate(stime,etime){
    var diffdate = new Array();
    var i=0;
    while(stime<=etime){
        diffdate[i] = stime;
        var stime_ts = new Date(stime).getTime();
        var next_date = stime_ts + (24*60*60*1000);
        var next_dates_y = new Date(next_date).getFullYear()+'-';
        var next_dates_m = (new Date(next_date).getMonth()+1 < 10)?'0'+(new Date(next_date).getMonth()+1)+'-':(new Date(next_date).getMonth()+1)+'-';
        var next_dates_d = (new Date(next_date).getDate() < 10)?'0'+new Date(next_date).getDate():new Date(next_date).getDate();
        stime = next_dates_y+next_dates_m+next_dates_d;
        i++;
    }
    return diffdate;
}

3.根据包含时间的对象降序
/**
 * @method timeDescendingOrder 根据时间降序排序包含时间对象的数组
 * @param {Array} arr 需要排序的包含时间对象的数组
 * @param {string} key 排序的时间字段
 * @return {Array} 根据指定时间字段降序排序的数组
 * */
function timeDescendingOrder(arr,key){
    arr.sort(function (a,b){
        var t1 = new Date(Date.parse(a[key].replace(/-/g,"/")));
        var t2 = new Date(Date.parse(b[key].replace(/-/g,"/")));
        return t2.getTime()-t1.getTime()
    });
    return arr
}
4.根据包含时间的对象升序
/**
 * @method timeDescendingOrder 根据时间升序排序包含时间对象的数组
 * @param {Array} arr 需要排序的包含时间对象的数组
 * @param {string} key 排序的时间字段
 * @return {Array} 根据指定时间字段升序排序的数组
 * */
function timeAscendingOrder(arr,key){
    arr.sort(function (a,b){
        return b[key] < a[key] ? 1 : -1
    });
    return arr
}


5.自定义格式时间格式化
/**
 * @method dateFormat 自定义格式时间格式化
 * @param {string} fmt 自定时间格式 如:YYYY-mm-dd HH:MM:SS
 * @param {string} t 时间对象
 * @return {string} 格式化后的时间
 * */
function dateFormat(fmt, t) {
    var date = new Date(t);
    var ret;
    var opt = {
        "Y+": date.getFullYear().toString(),
        "m+": (date.getMonth() + 1).toString(),
        "d+": date.getDate().toString(),
        "H+": date.getHours().toString(),
        "M+": date.getMinutes().toString(),
        "S+": date.getSeconds().toString()
    };
    for (var k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
    }
    return fmt;
}


数组类


1.检查数组是否包含重复项
/**
 * @method arrIsRepeat 验证数组是否有重复
 * @param {Array} arr 需要验证是否重复
 * @return {boolean} 数组是否有重复项
 * */
function arrIsRepeat(arr){
    var hash = {};
    for(var i in arr) {
        if(hash[arr[i]])
            return true;
        hash[arr[i]] = true;
    }
    return false;
}

先更这么多

最新喜欢:

37.5°37.5°
游客


返回顶部

公众号

公众号