js工具类

/**
 * 转换日期格式 比如 2015-02-02转换为20150202
 * 
 * @param date
 * @returns {String}
 */
function getDate(datestr) {
	var dates = datestr.split("-");
	var dateReturn = '';

	for (var i = 0; i < dates.length; i++) {
		dateReturn += dates[i];
	}
	return dateReturn;
}

/**
 * 将日期格式转为日期字符串格式 eg:2015-06-19 10:23 to 201506191023
 */
function dateToStr(dateStr) {
	if (null != dateStr && "" != $.trim(dateStr)) {
		dateStr = $.trim(dateStr);
		return dateStr.replace(/-/g, '').replace(/ /g, '').replace(/:/g, '');
	} else {
		return "";
	}
}


/**
 * 字符串转日期格式 eg: str = 20150421094703 return 2015-04-21 09:47:03 eg: str =
 * 20150421 return 2015-04-21 其他返回 "";
 * 
 * @param str
 *            日期字符串
 * @param hasTime
 *            是否要包含时间,不包含则只显示日期
 * @returns {String}
 */
function strToFormartDate(str, hasTime) {
	var date_tag = "-";
	var time_tag = ":";
	if (null != str && "" != $.trim(str)) {
		str = $.trim(str);
		if (hasTime) {
			if (str.length == 8) {
				return str.substr(0, 4) + date_tag + str.substr(4, 2)
						+ date_tag + str.substr(6, 2);
			} else if (str.length == 12) {
				return str.substr(0, 4) + date_tag + str.substr(4, 2)
						+ date_tag + str.substr(6, 2) + " " + str.substr(8, 2)
						+ time_tag + str.substr(10, 2);
			} else if (str.length >= 14) {
				return str.substr(0, 4) + date_tag + str.substr(4, 2)
						+ date_tag + str.substr(6, 2) + " " + str.substr(8, 2)
						+ time_tag + str.substr(10, 2) + time_tag
						+ str.substr(12, 2);
			} else {
				return "";
			}
		} else {
			return str.substr(0, 4) + date_tag + str.substr(4, 2) + date_tag
					+ str.substr(6, 2);
		}
	} else {
		return "";
	}
}


/*
 * 空判断
 */
function isEmpty(str) {
	var val = $.trim(str);
	if (val == null)
		return true;
	if (val == undefined || val == 'undefined')
		return true;
	if (val == "")
		return true;
	if (val.length == 0)
		return true;
	if (!/[^(^\s*)|(\s*$)]/.test(val))
		return true;
	return false;
}


/*
 * 非空判断
 */
function isNotEmpty(str) {
	return !isEmpty(str);
}



// 获取当前时间精确到分
function getClientCurrentTime() {
	var now = new Date();
	var year = now.getFullYear(); // 年
	var month = now.getMonth() + 1; // 月
	var day = now.getDate(); // 日

	var hh = now.getHours(); // 时
	var mm = (now.getMinutes()) % 60; // 分
	if ((now.getMinutes()) / 60 > 1) {
		hh += Math.floor((now.getMinutes()) / 60);
	}

	var clock = year + "-";

	if (month < 10)
		clock += "0";

	clock += month + "-";

	if (day < 10)
		clock += "0";

	clock += day + " ";

	if (hh < 10)
		clock += "0";

	clock += hh + ":";
	if (mm < 10)
		clock += '0';
	clock += mm;
	return (clock);
}

 

posted @ 2015-12-03 09:30  深呼吸000  阅读(164)  评论(0编辑  收藏  举报