`
steven-zhou
  • 浏览: 207260 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

日期处理通用函数

阅读更多
var DateUtils_MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
/** 时间处理公共函数 */
function DateUtils() {}

DateUtils.prototype.toDate = function(strDate) {
	if (strDate.length == 19) {	// YYYY-MM-DD HH:MI:SS
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		var date  = strDate.substring(8, 10);
		var hour  = strDate.substring(11, 13);
		var min   = strDate.substring(14, 16);
		var sec   = strDate.substring(17, 19);
		return new Date(year, month - 1, date, hour, min, sec);
	} else if (strDate.length == 10) { // "YYYY-MM-DD"
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		var date  = strDate.substring(8, 10);
		return new Date(year, month - 1, date);
	} else if (strDate.length == 7) { // "YYYY-MM"
		var year  = strDate.substring(0, 4);
		var month = strDate.substring(5, 7);
		return new Date(year, month - 1);
	} else if (strDate.length == 4) { // "YYYY"
		var year  = strDate.substring(0, 4);
		return new Date(year);		
	} else {
		alert("DateUtils.toDate(strDate) error! invalid argument(format).");
	}
}

DateUtils.prototype.toString = function(date, format) {
	var strDate;
	var year  = date.getFullYear();
	var month = date.getMonth() + 1;
	var day   = date.getDate();
	var hour  = date.getHours();
	var min   = date.getMinutes();
	var sec   = date.getSeconds();
	month = (parseInt(month) < 10) ? ("0" + month) : (month);
	day   = (parseInt(day)   < 10) ? ("0" + day )  : (day);
	hour  = (parseInt(hour)  < 10) ? ("0" + hour)  : (hour);
	min   = (parseInt(min)   < 10) ? ("0" + min)   : (min);
	sec   = (parseInt(sec)   < 10) ? ("0" + sec)   : (sec);
	if ("YYYY-MM-DD HH:MI:SS" == format) {
		strDate = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
	} else if ("YYYY-MM-DD" == format) {
		strDate = year + "-" + month + "-" + day;
	} else if ("YYYY-MM" == format) {
		strDate = year + "-" + month;	
	} else if ("YYYY" == format) {
		strDate = year;		
	} else {
		alert("DateUtils.toString(date, format) error! invalid argument(format).");
	}
	return strDate;
}

DateUtils.prototype.getMonthDays = function(date,month) {
	var year = date.getFullYear();
	if (typeof month == "undefined") {
		month = date.getMonth();
	}
	if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
		return 29;
	} else {
		return DateUtils_MD[month];
	}
};
	
DateUtils.prototype.addDays = function(dayOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
	date = new Date(date.getTime() + parseInt(dayOffset) * 24 * 3600 * 1000);
	return this.toString(new Date(date), "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addMonths = function(monthOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()): this.toDate(strBaseDate);
	var month=date.getMonth();
	var cd=date.getDate();//this.getMonthDays(date,month);
	var td=this.getMonthDays(date,date.getMonth() + parseInt(monthOffset));
	if(cd > td){date.setDate(td);}
	date.setMonth(date.getMonth() + parseInt(monthOffset));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addMonthsForStart = function(monthOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addMonths(monthOffset, strDate);
	return this.firstDayOfMonth(strDate);
}

DateUtils.prototype.addMonthsForEnd = function(monthOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addMonths(monthOffset, strDate);
	return this.addDays(-1, this.firstDayOfMonth(strDate));
}

DateUtils.prototype.addYears = function(yearOffset, strBaseDate) {
	var date = (arguments.length == 1) ? this.toDate(this.today()) : this.toDate(strBaseDate);
	date.setYear(date.getYear() + parseInt(yearOffset));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.addYearsForStart = function(yearOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addYears(yearOffset, strDate);
	return this.firstDayOfYear(strDate);
}

DateUtils.prototype.addYearsForEnd = function(yearOffset, strBaseDate) {
	var strDate = (arguments.length == 1) ? this.today() : strBaseDate;
	strDate = this.addYears(yearOffset, strDate);
	return this.firstDayOfYear(strDate);
}

DateUtils.prototype.sunOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay()) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");	
}

DateUtils.prototype.monOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 1) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.tueOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 2) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.wedOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 3) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.turOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 4) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.friOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 5) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.satOfWeek = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date = new Date(date - (date.getDay() - 6) * (24 * 3600 * 1000));
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.firstDayOfMonth = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setDate(1);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.lastDayOfMonth = function(strDate) {
	strDate = (arguments.length == 0) ? this.today() : (strDate);
	strDate = this.addMonths(1, strDate);
	strDate = this.firstDayOfMonth(strDate);
	strDate = this.addDays(-1, strDate);
	return strDate;
}

DateUtils.prototype.firstDayOfYear = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setMonth(0);
	date.setDate(1);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.lastDayOfYear = function(strDate) {
	var date = (arguments.length == 0) ? this.toDate(this.today()) : this.toDate(strDate);
	date.setMonth(11);
	date.setDate(31);
	return this.toString(date, "YYYY-MM-DD HH:MI:SS");
}

DateUtils.prototype.today = function(format) {
	if(getToday && typeof(getToday)=="function"){
		return getToday();
	}else{
		if (arguments.length == 0) {
			return this.toString(new Date(), "YYYY-MM-DD");
		} else {
			return this.toString(new Date(), format);
		}
	}
}
分享到:
评论
1 楼 cuixiping 2010-04-08  
不大实用。
只要有 dateAdd, dateDiff 差不多就都够用了

相关推荐

    oracle函数总结

    oracle函数总结——字符函数、数值函数、日期函数、转换函数、通用函数、事务处理等

    C语言通用范例开发金典.part1.rar

    ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.8 中序遍历二叉树(顺序结构) 169 范例1-62 中序遍历二叉树 169 ∷相关函数:InOrderTraverse函数 1.4.9 中序遍历...

    表的相关查询(最全面的查询语句集合)

    --SQL2000/2005字符串拆分为列表通用函数 --SQL2005以上版本可以结合apply进行拆分列值 --4、FOR XML PATH 语句的应用 --5、一些有用的系统存储过程及用法 --6、关于死锁 --7、添加远程服务器 --8、日期的相关操作 -...

    C语言通用范例开发金典.part2.rar

    ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.7 双亲、孩子和兄弟节点的查询(链式结构) 162 范例1-61 双亲、孩子和兄弟节点的查询 162 ∷相关函数:Parent函数 ...

    -C++参考大全(第四版) (2010 年度畅销榜

    18.2 应用通用函数 18.3 通用类 18.4 关键字typename和export 18.5 模板的功用 第19章 异常处理 19.1 异常处理基础 19.2 处理派生类异常 19.3 异常处理选项 19.4 理解terminate()和unexpected() 19.5 uncaught_...

    Oracle 11g视频教程

    day05_转换函数、通用函数、条件表达式 day06_多表联接 day07_组函数 day08_子查询 day09_替代变量SQLplus环境命令 day10_DML语句的使用 day11_事务的概念和处理事务 day12_创建表、管理表 day13_管理约束 day14_...

    Sqlserver2000经典脚本

    ├─第02章 │ │ 2.1 日期概念理解中的一些测试.sql │ │ 2.2.4 CONVERT在日期转换中的使用示例.sql │ │ 2.3.3 SET DATEFORMAT对日期处理的影响.sql │ │ 2.3.4 SET LANGUAGE对日期处理...

    【VB】VB通用开发金典(完整版)学VB的必备资料,实例,源码。

    范例1-37 状态栏显示系统时间和日期 ∷相关函数:Now 范例1-38 循环滚动文字的状态栏 ∷相关函数:Right cLeft 范例1-39 状态栏按钮的灰度显示 范例1-40 显示文本行数的状态栏 ∷相关函数:SendMessage ...

    经典SQL脚本大全

    │ │ 2.3.3 SET DATEFORMAT对日期处理的影响.sql │ │ 2.3.4 SET LANGUAGE对日期处理的影响示例.sql │ │ 2.4.1 日期格式化处理.sql │ │ 2.4.2 日期推算处理.sql │ │ 2.4.3 特殊日期加减函数.sql │ │ 2.5.1...

    C 开发金典

    C 语言通用范例开发金典 第1章 数据结构. 1 1.1 数组和字符串 2 1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维...

    java古董级工具 构建工具 字节码操作 集群管理 代码分析 编译器生成工具 外部配置工具 约束满足问题求解程序 持续集成 CS

    文档处理工具 函数式编程 游戏开发 GUI 高性能计算 IDE 图像处理 IDE 日志 机器学习 消息传递 杂项 应用监控工具 原生开发库 网络 ORM PDF 性能分析 响应式开发库 REST框架 科学计算与分析 搜索引擎 安全 序列化 ...

    C#公共类通用类非常齐全

    8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅助类(AudioHelper.cs) 摄像头操作辅助类,包括开启、关闭、抓图、设置等功能(Camera.cs...

    通用FormValid1.0-js验证框架

    isDate 是否为日期格式 isEmail 必须是Email格式 isPhone 是否为电话号码 isMobile 是否为手机号码 isTelephone 是否为电话或手机号码 isIdCard 是否为身份证号码 isMoney 是否为货币值 isZip 是否是邮件编码 isQQ ...

    ucrtbased.dll..

    CRT 库被重构为两个不同的二进制文件、一个通用 CRT (ucrtbase)(其中包含大多数标准功能)和一个 VC 运行时库 (vcruntime140)(其中包含与编译器相关的功能,如异常处理和内部函数)。 系统缺乏正确版本的调试版...

    通用Android工具库Common4Android.zip

    日期工具类,日期转换生肖、日期转换星座、日期相互转换。 DesUtil.java DES加密工具类。 DeviceUtil.java 设备信息获取工具类,获得设备型号、...

    JavaScript经典实例

     7.3创建一个通用的、可重用的事件处理函数  7.4根据修改的条件来取消一个事件  7.5阻止事件在一组嵌套元素中传播  7.6捕获键盘活动  7.7使用新的HTML 5拖放  7.8使用Safari方向事件和其他移动开发环境  第8...

    oracle公司内部的培训资料

    Les03 : 单行函数[字符/数值/日期/转换/通用] Les04 : 多表查询 Les05 : 分组函数 Les06 : 子查询 Les07 : iSQL*Plus Les08 : 处理数据[DML:UPDATE/INSERT INTO/DELETE FROM] Les09 : 创建和管理表[CREATE/ALTER/...

    powerbuilder

    需要注意的是,使用PrintOpen()函数打开打印作业、使用同组的其它函数完成打印任务后,必须使用PrintClose()关闭打印作业,或根据需要使用PrintCancel()函数取消打印作业。 ------------------------------------...

Global site tag (gtag.js) - Google Analytics