<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<style type="text/css">
.bar{
FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=darkorange,endColorStr=darkorange);
position:absolute;
border:1px solid #eee;
}
</style>
<script type="text/javascript">
//<![CDATA[
function makeDiv(height,width,left,top){
}
var DateSegment = function(from,to){
//this.from = new Date();
//this.to = new Date();
var dvArr = from.split('/');
this.from = new Date(parseInt(dvArr[2], 10), parseInt(dvArr[0], 10) - 1, parseInt(dvArr[1], 10),0,0,0);
dvArr = to.split('/');
this.to = new Date(parseInt(dvArr[2], 10), parseInt(dvArr[0], 10) - 1, parseInt(dvArr[1], 10),0,0,0);
this.toString = function(){
var _s = "";
_s += ("[" + this.from.getYear() + "\/" + (this.from.getMonth() + 1) + "\/" + this.from.getDate() + " to ");
_s += (this.to.getYear() + "\/" + (this.to.getMonth() + 1) + "\/" + this.to.getDate() + "]");
return _s + " ";
}
this.iFrom = Date.parse(this.from);
this.iTo = Date.parse(this.to);
}
var SortDateSeg = function(iDate, oDate){
this.iDate = iDate;
this.oDate = oDate;
}
function Task(from, to, task, resource)
{
var _from = new Date();
var _to = new Date();
var _task = task;
var _resource = resource;
var dvArr = from.split('/');
_from.setFullYear(parseInt(dvArr[2], 10), parseInt(dvArr[0], 10) - 1, parseInt(dvArr[1], 10));
dvArr = to.split('/');
_to.setFullYear(parseInt(dvArr[2], 10), parseInt(dvArr[0], 10) - 1, parseInt(dvArr[1], 10));
this.getFrom = function(){ return _from};
this.getTo = function(){ return _to};
this.getTask = function(){ return _task};
this.getResource = function(){ return _resource};
}
var DateSpliter = {
dateSegList : new Array(),
addDateSegment : function(from,to){
this.dateSegList.push(new DateSegment(from, to));
},
split : function(dateSegList){
var splitedSegments = new Array();// an array to store the splited segments
var sortedDates = this.sortByFromAndTo(dateSegList);
var len = sortedDates.length;
var i = 0;
while(i < len){
var seg = {}; //splited segment
var o = sortedDates[i];
seg.dateSegments = new Array(); //store the dateSegment objects that in this split segment
seg.dateSegments.push(o.oDate); //store the "from" dateSegment object
seg.from = new Date(o.iDate); //store the from date
while((++i < len) && sortedDates[i].iDate == o.iDate && sortedDates[i].iType == 0)
seg.dateSegments.push(sortedDates[i].oDate);
if(i == len){
seg.to = seg.from;
}else{
var o2 = sortedDates[i];
var hasFromTypeDate = false;
while((i < len) && sortedDates[i].iDate == o2.iDate){
if(sortedDates[i].iType == 0){
hasFromTypeDate = true; //找到一个开始节点,将把现在的段切断,结束节点似乎只需要保持一个
break;
}
++i;
}
if(i == len){
if(!hasFromTypeDate)
seg.to = o2.oDate.to;
else{
//complete the current segment
seg.to = new Date();
seg.to.setTime(o2.iDate - 86400000);
//add new segments because some segments were cutted by a later date.
--i;//move i from position len to len-1
for( var s in seg.dateSegments){
//create a new date node and store it in sortedDates as a new from-date of the rest of the cutted dateSegments
//
var temp = {};
temp.iDate = o2.iDate;
temp.iType = 0; //from mark
temp.oDate = seg.dateSegments[s];
sortedDates[--i] = temp;
}
}
}else{
if(!hasFromTypeDate){
seg.to = o2.oDate.to;
for( var s in seg.dateSegments){
if(seg.dateSegments[s].iTo > o2.iDate){ //if some seg's to date is larger than current split seg's to date
var temp = {};
temp.iDate = o2.iDate + 86400000;
temp.iType = 0;
temp.oDate = seg.dateSegments[s];
sortedDates[--i] = temp;
}
}
}else{
seg.to = new Date();
seg.to.setTime(o2.iDate - 86400000);
//add new segments because some segments were cutted by a later date.
//should keep vertical line that iDate == o2.iDate as the new from node
for( var s in seg.dateSegments){
//create a new date node and store it in sortedDates as a new from-date of the rest of the cutted dateSegments
//
var temp = {};
temp.iDate = o2.iDate;
temp.iType = 0; //from mark
temp.oDate = seg.dateSegments[s];
sortedDates[--i] = temp;
}
}
}
}
splitedSegments.push(seg);
}
return splitedSegments;
},
sortByFromAndTo : function(dateSegList){
var _list = [];
for(var i in dateSegList){
/*
var o1={},o2={};
o1.iDate = dateSegList[i].iFrom;
o1.oDate = dateSegList[i];
o2.iDate = dateSegList[i].iTo;
o2.oDate = dateSegList[i];
_list.push(o1);
_list.push(o2);
*/
_list.push({iDate : dateSegList[i].iFrom, oDate : dateSegList[i], iType : 0});
_list.push({iDate : dateSegList[i].iTo, oDate : dateSegList[i], iType : 1});
//_list.push(new SortDateSeg(dateSegList[i].iFrom, dateSegList[i]));
//_list.push(new SortDateSeg(dateSegList[i].iTo, dateSegList[i]));
//_list.push(dateSegList[i].iFrom);
//_list.push(dateSegList[i].iTo);
}
_list = _list.sort(function(a,b){
return (a.iDate - b.iDate);
});
//sort by 'iType' based on the sort upside.
var sIndex = 0,eIndex = 0;
for(var i = 1; i < _list.length; ++i){
if( _list[i].iDate == _list[sIndex].iDate)
eIndex = i;
else{
if(eIndex > sIndex){
var s = sIndex, e = eIndex;
while(s < e){
while(s < e && 0 == _list[s].iType) ++s;
while(s < e && 1 == _list[e].iType) --e;
if(s < e){
var t = _list[s];
_list[s] = _list[e];
_list[e] = t;
++s;
--e;
}
}
}
sIndex = i;
}
}
//最后几个相等的情况
if(eIndex > sIndex){
var s = sIndex, e = eIndex;
while(s < e){
while(s < e && 0 == _list[s].iType) ++s;
while(s < e && 1 == _list[e].iType) --e;
if(s < e){
var t = _list[s];
_list[s] = _list[e];
_list[e] = t;
++s;
--e;
}
}
}
return _list;
},
sortByFrom : function(dateSegList){
return ([].concat(dateSegList)).sort(function(a,b){
return a.iFrom - b.iFrom;
});
}
};
window.onload = function(){
/*
DateSpliter.addDateSegment("6/13/2009","12/13/2009");
DateSpliter.addDateSegment("7/19/2009","9/13/2009");
DateSpliter.addDateSegment("5/19/2009","6/13/2009");
DateSpliter.addDateSegment("12/5/2009","12/5/2009");
DateSpliter.addDateSegment("12/4/2009","12/14/2009");
*/
DateSpliter.dateSegList = [];
var d1,d2;
d1=new Date();
for(i=0;i<50 ;i++){
var m = Math.floor(Math.random()*12 + 1);
var d = Math.floor(Math.random()*30 + 1);
var m2 = Math.floor(Math.random()*(12 - m) + m);
var d2 = (m2 > m) ? (Math.floor(Math.random()*30 + 1)) : (Math.floor(Math.random()*(30-d) + d));
var s1 = m + "/" + d + "/" + "2009";
var s2 = m2 + "/" + d2 + "/" + "2009";
DateSpliter.addDateSegment(s1,s2);
}
d2=new Date();
var _s = "create:" + (d2-d1) +"ms, ";
Date.prototype.toString = function(){
return this.getYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate();
}
var origin = document.getElementById("origin");
var splited = document.getElementById("splited");
var sorted = document.getElementById("sorted");
d1 = new Date();
origin.innerH
没有合适的资源?快使用搜索试试~ 我知道了~
javascript时间段分割
共1个文件
htm:1个
4星 · 超过85%的资源 需积分: 42 20 下载量 171 浏览量
2009-02-04
12:40:31
上传
评论
收藏 3KB RAR 举报
温馨提示
对于多个时间段, 按照他们相交的部分分割成多个时间段
资源推荐
资源详情
资源评论
收起资源包目录
cut_date.rar (1个子文件)
cut_date.htm 9KB
共 1 条
- 1
资源评论
- 酷尼尔2012-09-11确实不错,挺实用的
- kehuanshijie22014-09-05总结的不错。
远涉江湖
- 粉丝: 76
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功