javascript 时间日期工具方法收藏 持续更新

原创  郑建华   2020-01-06   76人阅读  0 条评论
摘要:

/** 比较两个日期的月份和年月是否相等 @return 相等返回true 不相等返回false  */ function compareDate(date1,date2){        var flag=false;        var d1=new Date(date1); &nbs

/**
	比较两个日期的月份和年月是否相等
	@return 相等返回true 不相等返回false
 */
function compareDate(date1,date2){
      	var flag=false;
      	var d1=new Date(date1);
      	var d2=new Date(date2);
      	if(d1.getFullYear() == d2.getFullYear()){
      		if(d1.getMonth() == d2.getMonth()){
      			flag=true;
      		}
      	}
      	return flag;
      }
/**
*    日期差
*/      
function dateDiff(date1, date2){       
        var type1 = typeof date1, type2 = typeof date2;       
        if(type1 == 'string')       
            date1 = stringToTime(date1);       
        else if(date1.getTime)       
            date1 = date1.getTime();       
        if(type2 == 'string')       
            date2 = stringToTime(date2);       
        else if(date2.getTime)       
            date2 = date2.getTime();   
        return (date2 - date1) / 1000 / 60 / 60 / 24;//除1000是毫秒,不加是秒   
}
//字符串转成Time毫秒数
function stringToTime(string){       
        var f = string.split(' ', 2);       
        var d = (f[0] ? f[0] : '').split('-', 3);       
        var t = (f[1] ? f[1] : '').split(':', 3);       
        return (new Date(       
        parseInt(d[0], 10) || null,       
        (parseInt(d[1], 10) || 1)-1,       
        parseInt(d[2], 10) || null,       
        parseInt(t[0], 10) || null,      
        parseInt(t[1], 10) || null,       
        parseInt(t[2], 10) || null)).getTime();   
}
//时间毫秒数转字符串
function timestampToTime(timestamp) {
		if(timestamp){
			var date = new Date(timestamp);
	        Y = date.getFullYear() + '-';
	        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	        D = date.getDate()<10 ? '0'+date.getDate() + ' ':date.getDate() + ' ';
	        h = date.getHours()<10 ? '0'+date.getHours() + ':':date.getHours() + ':';
	        m = date.getMinutes()<10 ? '0'+date.getMinutes():date.getMinutes();
	        return Y+M+D;
		}else{
			return "";
		}
    }
//将时间为个位数时增加'0',即'09:10'
function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

/**
         * 转换日期对象为字符串
         * @param date 日期对象
         * @param flag 返回类型标记;true:yyyy-MM-dd;false: yyyy-MM-dd HH:mm:ss
         */
function  convertDate2String(date, flag) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hour = checkTime(date.getHours());
            var min = checkTime(date.getMinutes());
            if (flag) {
                return year + "-" + month + "-" + day + " ";
            } else {
                return year + "-" + month + "-" + day + " " + hour + ":" + min;
            }
}
/**
         * 返回与指定日期相差指定时间差的日期
         * @param interval 要转换的时间类型:y:年;q:季度;m:月;w:周;d:天;hh:时;mm:分;ss:秒
         * @param date 要转换的日期
         * @param number 转换的时间
         * @param flag true:增加;false:减
         * @returns {string}
         */
function getDaydiffDate(interval, number, flag, date) {
            switch (interval) {
                case "y ":
                {
                    if (flag)
                        date.setFullYear(date.getFullYear() + number);
                    else
                        date.setFullYear(date.getFullYear() - number);
                    return date;
                    break;
                }
                case "q ":
                {
                    if (flag)
                        date.setMonth(date.getMonth() + number * 3);
                    else
                        date.setMonth(date.getMonth() - number * 3);
                    return date;
                    break;
                }
                case "m ":
                {
                    if (flag)
                        date.setMonth(date.getMonth() + number);
                    else
                        date.setMonth(date.getMonth() - number);
                    return date;
                    break;
                }
                case "w ":
                {
                    if (flag)
                        date.setDate(date.getDate() + number * 7);
                    else
                        date.setDate(date.getDate() - number * 7);
                    return date;
                    break;
                }
                case "d ":
                {
                    if (flag)
                        date.setDate(date.getDate() + number);
                    else
                        date.setDate(date.getDate() - number);
                    return date;
                    break;
                }
                case "hh":
                {
                    if (flag)
                        date.setHours(date.getHours() + number);
                    else
                        date.setHours(date.getHours() - number);
                    return date;
                    break;
                }
                case "mm":
                {
                    if (flag)
                        date.setMinutes(date.getMinutes() + number);
                    else
                        date.setMinutes(date.getMinutes() - number);
                    return date;
                    break;
                }
                case "ss":
                {
                    if (flag)
                        date.setSeconds(date.getSeconds() + number);
                    else
                        date.setSeconds(date.getSeconds() - number);
                    return date;
                    break;
                }
                default:
                {
                    if (flag)
                        date.setDate(date.getDate() + number);
                    else
                        date.setDate(date.getDate() - number);
                    return date;
                    break;
                }

            }
}
/**
         * 重新格式化日期字符串,排除时间后带“.0”的格式,如“2015-11-11 12:12:20.0”,并通过标记判断是否显示时间
         * @param dateString 日期字符串
         * @param flag true:显示时间;false:不显示时间
 */
function reformateDateString(dateString, flag) {
            if(dateString){
                if (dateString.indexOf(".0") != -1) {
                    dateString = dateString.replace(".0", "");
                }

                var regEx = new RegExp("\\-", "gi");
                dateString = dateString.replace(regEx, "/");
                if (!flag) {

                    dateString = commonutil.date.convertDate2String(new Date(dateString), true);
                }
            }
            return dateString;
 }
/js比较日期大小
function dateCompare(startdate, enddate) {
        var arr = startdate.split("-");
        var starttime = new Date(arr[0], arr[1], arr[2]);
        var starttimes = starttime.getTime();

        var arrs = enddate.split("-");
        var lktime = new Date(arrs[0], arrs[1], arrs[2]);
        var lktimes = lktime.getTime();

        if (starttimes > lktimes) {
            return false;
        }else{
            return true;
         }
}


本文地址:https://www.zjh336.cn/?id=159
版权声明:本文为原创文章,版权归 郑建华 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?