1 /**
  2                 @name Util.DateTimeUtil
  3                 @class A utility class for doing date time related formatting and conversions
  4 **/
  5 
  6 var  DateTimeUtil = {
  7 	epochOffset : 0,
  8 
  9 	addProtoToDate : function()
 10 	{
 11 		Date.prototype.stdTimezoneOffset = function() {
 12  		   var jan = new Date(this.getFullYear(), 0, 1);
 13 		    var jul = new Date(this.getFullYear(), 6, 1);
 14 		    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
 15 		}	
 16 
 17 		Date.prototype.dst = function() {
 18 		    return this.getTimezoneOffset() < this.stdTimezoneOffset();
 19 		}
 20 
 21 	},
 22 
 23 	convertDateToGMTDate : function(date)
 24 	{
 25 		// change updated date to IST format.
 26 		var offset=new Date().getTimezoneOffset() * 60000;
 27 		var d=new Date(date.getTime()+offset);
 28 		return d;
 29 
 30 	},
 31 	convertDateFromGMTDate : function(date)
 32 	{
 33 		// change updated date to IST format.
 34 		var offset=new Date().getTimezoneOffset() * 60000;
 35 		var d=new Date(date.getTime()-offset);
 36 		return d;
 37 
 38 	},
 39 	// convert a GMT dateString to locatem date strinig
 40 
 41 	getOffset : function(dateString)
 42 	{
 43 		 var date = new Date();
 44                 var  offsetT = date.getTimezoneOffset();
 45                 var bookedDate  = this.stringToDate(dateString);
 46                 var offset;
 47                 if((bookedDate.dst() && date.dst()) || (!bookedDate.dst() && !date.dst())) offset =offsetT;
 48                 else if(bookedDate.dst() && !date.dst()) offset = offsetT-60;
 49 		else offset = offsetT + 60;
 50 		return (offset* 60000);
 51 		//                var timeMillis=new Date(this.stringToDate(dateString).getTime()-offset).getTime();
 52 
 53 
 54 	},
 55 
 56 	convertStringFromGMTString : function(dateString,useEpochAdjust)
 57 	{
 58 	// change updated date to IST format.
 59 		var offset= this.getOffset(dateString);
 60 		//if((bookedDate.dst() && date.dst()) || (!bookedDate.dst() && !date.dst())) offset =offsetT;
 61 		//else if(bookedDdate.dst() && !date.dst() ? offsetT+60 :  offsetT) * 60000
 62 		var timeMillis=new Date(this.stringToDate(dateString).getTime()-offset).getTime();
 63 		if(useEpochAdjust)
 64 			timeMillis = timeMillis + this.epochOffset;
 65 		var d = new Date(timeMillis);
 66 		if(!(d.getTime() > 0)) d = new Date(); //default to current date
 67 		var hh = d.getHours(); if(hh<10 && (""+hh).indexOf('0')!=0) hh = "0"+hh;
 68 		var mm = d.getMinutes(); if(mm<10 && (""+mm).indexOf('0')!=0) mm = "0"+mm;
 69 		var ss = d.getSeconds(); if(ss<10 && (""+ss).indexOf('0')!=0) ss = "0"+ss;
 70 		if(mm=='0') mm='00';
 71 		if(ss=='0') ss='00';
 72 		if(hh=='0') hh='00';
 73 		var month = this.parseInt(d.getMonth()+1);
 74 		if(month <10 && (""+month).indexOf('0')!=0) month = "0"+month;
 75 		
 76 
 77 		var year = d.getFullYear();
 78 		//if(month==12) year = year-1;
 79 		var date = d.getDate(); if(date<10 && (""+date).indexOf('0')!=0) date = "0"+date;
 80 		return (year+"-"+month+"-"+date+" " +hh+":"+mm+":"+ss);
 81 	},
 82 	// convert a localDateString to GMT string
 83 	convertStringToGMTString : function(dateString)
 84 	{
 85 		 var date = new Date();
 86                 var  offsetT = date.getTimezoneOffset();
 87                 var bookedDate  = this.stringToDate(dateString);
 88                 //var offset=(bookedDate.dst() ? offsetT+60 :  offsetT) * 60000;
 89 		 var offset= this.getOffset(dateString);
 90                 var timeMillis=new Date(this.stringToDate(dateString).getTime()+offset).getTime();
 91 
 92 
 93 	
 94 		var d = new Date(timeMillis);
 95 		if(!(d.getTime() > 0)) d = new Date(); //default to current date
 96 		var hh = d.getHours(); if(hh<10 && (""+hh).indexOf('0')!=0) hh = "0"+hh;
 97 		var mm = d.getMinutes(); if(mm<10 && (""+mm).indexOf('0')!=0) mm = "0"+mm;
 98 		var ss = d.getSeconds(); if(ss<10 && (""+ss).indexOf('0')!=0) ss = "0"+ss;
 99 
100 		if(mm=='0') mm='00';
101 		if(ss=='0') ss='00';
102 		if(hh=='0') hh='00';
103 
104 		var month = this.parseInt(d.getMonth()+1);
105 		if(month <10 && (""+month).indexOf('0')!=0) month = "0"+month;
106 
107 			
108 		var year = d.getFullYear();
109 		//if(month==12) year = year-1;
110 		var date = d.getDate(); if(date<10 && (""+date).indexOf('0')!=0) date = "0"+date;
111 		return (year+"-"+month+"-"+date+" " +hh+":"+mm+":"+ss);
112 
113 	},
114 	// conver string to date object
115 	stringToDate : function(updated)
116 	{
117 		var dateTime=updated.split(" ");
118 		var date=dateTime[0];
119 		var time=dateTime[1];
120 							
121 		var dateArray=(date.indexOf('-')> -1) ? date.split("-") : date.split('/');
122 		var timeArray=time.split(":");
123 				
124 		var dateObj=new Date(dateArray[0],dateArray[1]-1,dateArray[2],timeArray[0],timeArray[1],timeArray[2]);
125 		return dateObj;
126 	},
127 	getStringFromDateObj : function(dateObj)
128 	{
129 		var secs = dateObj.getSeconds();
130 		if( secs < 10 ){ secs = "0" + secs; }	
131 		
132 		var mins = dateObj.getMinutes();
133 		if( mins < 10 ){ mins = "0" + mins; }	
134 
135 		var hours = dateObj.getHours();
136 		if( hours < 10 ){ hours = "0" + hours; }	
137 
138 		var day_of_month = dateObj.getDate();
139 		if( day_of_month < 10 ){ day_of_month = "0" + day_of_month; }
140 
141 		var month = dateObj.getMonth();
142 		month++;
143 		if( month < 10 ){ month = "0" + month; }
144 	
145 		return dateObj.getFullYear()+'-'+ month + '-' + day_of_month + " " + hours + ':' + mins + ':' + secs;
146 
147 	},
148 	getStringFromDateTimeString : function(dateString, timeString,appendSecs)
149 	{
150 		return dateString + " " + timeString +(appendSecs ? ':00' : '');
151 	
152 	},
153 	getDateObjFromDateTimeString : function(dateString, timeString)
154 	{
155 		  var date=dateString;
156                 var time=timeString;
157 
158                 var dateArray=(date.indexOf('-')> -1) ? date.split("-") : date.split('/');
159 
160                 var timeArray=time.split(":");
161 			for(var i=0;i<dateArray.length;i++)
162 				{
163 				if(dateArray[i].indexOf('0')=='0') dateArray[i]=dateArray[i].substr(1,dateArray[i].length-1);
164 				}
165 			for(var i=0;i<timeArray.length;i++)
166 				{
167 				if(timeArray[i].indexOf('0')=='0') timeArray[i]=timeArray[i].substr(1,timeArray[i].length-1);
168 				}
169 
170                 var dateObj=new Date( this.parseInt(dateArray[0]), this.parseInt(dateArray[1]) - 1, this.parseInt(dateArray[2]), this.parseInt(timeArray[0]), this.parseInt(timeArray[1]), 0 );
171 		return dateObj;
172 	},
173 	getDateObjFromDateString : function(dateString)
174 	{
175 			var arr = dateString.split(" ");
176 			
177 		  var date=arr[0];
178                 var time="";
179 			if(arr.length>1) time = arr[1];
180 			else time = " 00:00";
181 
182                 var dateArray=(date.indexOf('-')> -1) ? date.split("-") : date.split('/');
183 
184                 var timeArray=time.split(":");
185 			for(var i=0;i<dateArray.length;i++)
186 				{
187 				if(dateArray[i].indexOf('0')=='0') dateArray[i]=dateArray[i].substr(1,dateArray[i].length-1);
188 				}
189 			for(var i=0;i<timeArray.length;i++)
190 				{
191 				if(timeArray[i].indexOf('0')=='0') timeArray[i]=timeArray[i].substr(1,timeArray[i].length-1);
192 				}
193 
194                 var dateObj=new Date( this.parseInt(dateArray[0]), this.parseInt(dateArray[1]) - 1, this.parseInt(dateArray[2]), this.parseInt(timeArray[0]), this.parseInt(timeArray[1]), 0 );
195 		return dateObj;
196 	},
197 	getMillisFromDateString : function(dateString)
198 	{
199 		var arr = dateString.split(" ");
200 			
201 		  var date=arr[0];
202                 var time="";
203 			if(arr.length>1) time = arr[1];
204 			else time = "00:00:00";
205 
206                 var dateArray=(date.indexOf('-')> -1) ? date.split("-") : date.split('/');
207 
208                 var timeArray=time.split(":");
209 			for(var i=0;i<dateArray.length;i++)
210 				{
211 				if(dateArray[i].indexOf('0')=='0') dateArray[i]=dateArray[i].substr(1,dateArray[i].length-1);
212 				}
213 			for(var i=0;i<timeArray.length;i++)
214 				{
215 				if(timeArray[i].indexOf('0')=='0') timeArray[i]=timeArray[i].substr(1,timeArray[i].length-1);
216 				}
217 
218                 var dateObj=new Date( this.parseInt(dateArray[0]), this.parseInt(dateArray[1]) - 1, this.parseInt(dateArray[2]), this.parseInt(timeArray[0]), this.parseInt(timeArray[1]), this.parseInt(timeArray[2]) );
219 		return dateObj.getTime();
220 	},
221 
222 	getDateObjFromTimeString : function(time)
223 	{
224 			
225                 var timeArray=time.split(":");
226 			
227 			for(var i=0;i<timeArray.length;i++)
228 				{
229 				if(timeArray[i].indexOf('0')=='0') timeArray[i]=timeArray[i].substr(1,timeArray[i].length-1);
230 				}
231 
232                 var dateObj=new Date(this.parseInt(timeArray[0]), this.parseInt(timeArray[1]), 0 );
233 		return dateObj;
234 	},
235 
236 	getFormatedDateStringFromString : function(dateString)
237 	{
238 	var time = "";
239 	if(dateString.indexOf(" ") > -1)
240 	{
241 		time = dateString.split(" ")[1];
242 		dateString = dateString.split(" ")[0];
243 	}
244 	dateString = dateString.split("-").join("/");
245 	
246 	var d= new Date(dateString);
247 	var days = { "0":"Sun", "1":"Mon", "2":"Tue", "3":"Wed", "4":"Thu", "5":"Fri", "6":"Sat"};
248 	var month = {"1":"Jan","2":"Feb","3":"March","4":"April","5":"May","6":"Jun","7":"July","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"};
249 	return (days[d.getDay()] +" "+month[d.getMonth()+1]+" "+d.getDate()+", "+d.getFullYear()+" "+ this.formatTimeFromString(time,"hh:mm:ss TT"));
250 
251 	},
252 	getLocalTimeZoneString : function()
253 	{
254 		var d=  new Date();
255 		var s= d.toString();
256 		return s.substr(s.indexOf('(')+1,s.length-2).replace(')',"");
257 	},
258     getLocalTimeZoneDisplay : function()
259     {
260         var hour=(new Date().getTimezoneOffset() * -1)/60;
261         if (hour<0)
262         return "GMT "+hour;
263         else
264         return "GMT +"+hour;
265     },
266  getLocalTimeZoneDisplayString : function()
267     {
268         
269 	var s = (new Date()).toTimeString();
270 	var i1 = s.indexOf("(");
271 	var i2 = s.indexOf(")");
272 	if(i1!=-1 && i2!=-1) return s.substr(i1+1, (i2-i1-1));
273 	else return DateTimeUtil.getLocalTimeZoneDisplay();
274 	},
275     getLocalTimeZoneOffsetMinutes : function()
276     {
277         return (new Date().getTimezoneOffset() * -1);
278     },
279 	getFormatedDuration :  function(endDate,startDate)
280 	{
281 	
282 		var timeDiff;
283 	    var difference = endDate.getTime() - startDate.getTime();
284 	 
285 	    var daysDifference = Math.floor(difference/1000/60/60/24);
286 	    difference -= daysDifference*1000*60*60*24;
287 	 
288 	    var hoursDifference = Math.floor(difference/1000/60/60);
289 	    difference -= hoursDifference*1000*60*60;
290 	 
291 	    var minutesDifference = Math.floor(difference/1000/60);
292 	    difference -= minutesDifference*1000*60;
293 	 
294 	    var secondsDifference = Math.floor(difference/1000);
295 	 
296 	    timeDiff =  daysDifference + 'day/s ' + (daysDifference < 2 ? (hoursDifference + 'hr/s ' + minutesDifference + 'min/s ' + secondsDifference + ' second/s ') : '');
297 	    var days = this.stringCheck(daysDifference, 'day');
298 	    var hours =  this.stringCheck(hoursDifference, 'hr');
299 	    var minutes =  this.stringCheck(minutesDifference, 'min');
300 	   // var seconds =  this.stringCheck(secondsDifference, 'second') ; 
301 	    //return timeDiff = days + hours + minutes + seconds;
302 		if(daysDifference >=2) {hours=""; minutes=""; } 	 
303 		if(daysDifference ==1) { 
304 			if(hours!="")  minutes=""; //dont show mins if days n hours preesnt.
305 
306 		}
307 		return (days + hours + minutes);	    
308 	},
309 	getFormatedDurationFromMins :  function(mins)
310         {
311 
312                 var timeDiff;
313             var difference = (this.parseInt(mins)*60*1000);//endDate.getTime() - startDate.getTime();
314 
315             var daysDifference = Math.floor(difference/1000/60/60/24);
316             difference -= daysDifference*1000*60*60*24;
317 
318             var hoursDifference = Math.floor(difference/1000/60/60);
319             difference -= hoursDifference*1000*60*60;
320 
321             var minutesDifference = Math.floor(difference/1000/60);
322             difference -= minutesDifference*1000*60;
323 
324             var secondsDifference = Math.floor(difference/1000);
325 
326             timeDiff =  daysDifference + 'day/s ' + (daysDifference < 2 ? (hoursDifference + 'hr/s ' + minutesDifference + 'min/s ' + secondsDifference + ' second/s ') : '');
327             var days = this.stringCheck(daysDifference, 'day');
328             var hours =  this.stringCheck(hoursDifference, 'hr');
329             var minutes =  this.stringCheck(minutesDifference, 'min');
330            // var seconds =  this.stringCheck(secondsDifference, 'second') ;
331             //return timeDiff = days + hours + minutes + seconds;
332                 if(daysDifference >=2) {hours=""; minutes=""; }
333                 if(daysDifference ==1) {
334                         if(hours!="")  minutes=""; //dont show mins if days n hours preesnt.
335 
336                 }
337                 return (days + hours + minutes);
338         },
339 	getFormatedDurationFromMillis :  function(millis)
340         {
341 
342                 var timeDiff;
343             var difference = (this.parseInt(millis));//endDate.getTime() - startDate.getTime();
344 
345             var daysDifference = Math.floor(difference/1000/60/60/24);
346             difference -= daysDifference*1000*60*60*24;
347 
348             var hoursDifference = Math.floor(difference/1000/60/60);
349             difference -= hoursDifference*1000*60*60;
350 
351             var minutesDifference = Math.floor(difference/1000/60);
352             difference -= minutesDifference*1000*60;
353 
354             var secondsDifference = Math.floor(difference/1000);
355 
356             timeDiff =  daysDifference + 'day/s ' + (daysDifference < 2 ? (hoursDifference + 'hr/s ' + minutesDifference + 'min/s ' + secondsDifference + ' second/s ') : '');
357             var days = this.stringCheck(daysDifference, 'day');
358             var hours =  this.stringCheck(hoursDifference, 'hr');
359             var minutes =  this.stringCheck(minutesDifference, 'min');
360             var seconds =  this.stringCheck(secondsDifference, 'second') ;
361             //return timeDiff = days + hours + minutes + seconds;
362                 if(daysDifference >=2) {hours=""; minutes=""; }
363                 if(daysDifference ==1) {
364                         if(hours!="")  minutes=""; //dont show mins if days n hours preesnt.
365 
366                 }
367                 return (days + hours + minutes + seconds);
368         },
369 	getFormatedDurationFromMillisAsString :  function(millis)
370         {
371 
372             var timeDiff;
373             var difference = (this.parseInt(millis));//endDate.getTime() - startDate.getTime();
374 
375             var hoursDifference = Math.floor(difference/1000/60/60);
376             difference -= hoursDifference*1000*60*60;
377 
378             var minutesDifference = Math.floor(difference/1000/60);
379             difference -= minutesDifference*1000*60;
380 
381             var secondsDifference = Math.floor(difference/1000);
382 		var h="";m="";s="";
383 		if(hoursDifference < 10) h = "0"+hoursDifference;
384 		else h = ""+hoursDifference;
385 		h = h+":";
386 	     if(minutesDifference < 10) m = "0"+minutesDifference;
387 		else m = "" + minutesDifference;
388 		m=m+":";
389 	     if(secondsDifference  < 10) s = "0"+secondsDifference ;
390 		else s = "" + secondsDifference ;
391 
392                 return (h + m+ s);
393         },
394 
395 	getDurationInSecondFromString :  function(stringV)
396         {
397 	    var a = stringV.split(":");
398 	    var secs=0;
399 		secs = (this.parseInt(a[0])*60*60) + (this.parseInt(a[1])*60) + (this.parseInt(a[2])); 
400            return secs;
401         },
402 
403 	
404 	stringCheck : function(diff, str)	//String check to append s
405 	{
406 		var val = this.parseInt(diff);
407 		if(val > 1)
408 		{
409 			str = val +' ' + str+'s'+' ';
410 		}else if(val == 1)
411 		{
412 			str = val +' '+ str + ' ';
413 		}else
414 		{
415 			str = '';
416 		}
417 		return str;
418 	},
419 	formatDateTime : function (formatDate, formatString) 
420 	{
421 		if(formatDate instanceof Date) {
422 			var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
423 			var yyyy = formatDate.getFullYear();			
424 			var m = formatDate.getMonth();			
425 			var mmm = months[m];
426 			var d = formatDate.getDate();
427 			var dd = d < 10 ? "0" + d : d;			
428 			var h = formatDate.getHours();
429 			var hh = h < 10 ? "0" + h : h;
430 			var n = formatDate.getMinutes();
431 			var nn = n < 10 ? "0" + n : n;
432 			var s = formatDate.getSeconds();
433 			var ss = s < 10 ? "0" + s : s;
434 			var tt = h > 11 ? "PM" : "AM";
435 			if(hh > 12) hh = hh-12; 
436 			if(hh < 10 ) { if (hh==0) hh='00'; else if((''+hh).indexOf('0')==-1) hh='0'+hh; }
437 
438 			formatString = formatString.replace(/yyyy/i, yyyy);			
439 			formatString = formatString.replace(/mmm/i, mmm);			
440 			formatString = formatString.replace(/dd/i, dd);			
441 			formatString = formatString.replace(/hh/i, hh);			
442 			formatString = formatString.replace(/mm/i, nn);			
443 			formatString = formatString.replace(/ss/i, ss);			
444 			formatString = formatString.replace(/TT/i, tt);
445 
446 			return formatString;
447 		} else {
448 			return "";
449 		}
450 	},
451 
452 	formatDate : function(formatDate,formatString)
453 	{
454 	if(formatDate instanceof Date) {
455 			var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
456 			var yyyy = formatDate.getFullYear();			
457 			var m = formatDate.getMonth();			
458 			var mmm = months[m];
459 			var d = formatDate.getDate();
460 			var dd = d < 10 ? "0" + d : d;			
461 			
462 			formatString = formatString.replace(/yyyy/i, yyyy);			
463 			formatString = formatString.replace(/mmm/i, mmm);			
464 			formatString = formatString.replace(/dd/i, dd);			
465 			
466 			return formatString;
467 		} else {
468 			return "";
469 		}
470 
471 	},
472 	formatTime : function(formatDate,formatString)
473 	{	
474 		var is12Hour=false;
475 		is12Hour = (typeof PartnerConfig!="undefined" && PartnerConfig.getKey("use_12Hour_format",true));
476 		
477 		if(formatDate instanceof Date) {
478 			
479 			var h = formatDate.getHours();
480 			var tt = h > 11 ? "PM" : "AM";
481 			var hh;
482 			if(h > 12) h = h-12; 	
483 
484 			hh = (h < 10 && tt=="AM" && !is12Hour) ? "0" + h : h;
485 			if(h==0 && is12Hour) hh ="12";
486 			var n = formatDate.getMinutes();
487 			var nn = n < 10 ? "0" + n : n;
488 			var s = formatDate.getSeconds();
489 			var ss = s < 10 ? "0" + s : s;
490 			//this.formatTimeFromString(hh+":"+nn+":"+ss,"hh:mm");
491 			formatString = formatString.replace(/hh/i, hh);			
492 			formatString = formatString.replace(/mm/i, nn);			
493 			formatString = formatString.replace(/ss/i, ss);			
494 			formatString = formatString.replace(/TT/i, tt);
495 
496 			return formatString;
497 		} else {
498 			return "";
499 		}
500 
501 	},
502 	formatTimeFromString : function(timeString,formatString) { 
503 			if(!(typeof PartnerConfig!="undefined" && PartnerConfig.getKey("use_12Hour_format",true))) return timeString;
504 			if(timeString.trim().indexOf(" ")>-1)
505 				timeString = timeString.split(" ")[0];
506 			var a = timeString.split(":");
507 			var h = this.parseInt(a[0]);
508 			var tt = h > 11 ? "PM" : "AM";
509 			var hh;
510 			if(h > 12) h = h-12; 	
511 
512 			hh = h;
513 			if(h==0) hh ="12";
514 			var n = this.parseInt(a[1]);
515 			var nn = n < 10 ? "0" + n : n;
516 			
517 			var s = this.parseInt(a[2]);
518 			var ss = s < 10 ? "0" + s : s;
519 			
520 			formatString = formatString.replace(/hh/i, hh);			
521 			formatString = formatString.replace(/mm/i, nn);			
522 			formatString = formatString.replace(/ss/i, ss);			
523 			formatString = formatString.replace(/TT/i, tt);
524 
525 			return formatString;
526 		
527 
528 	},
529 	getTimeFromFormattedString :  function(timeString,is12HourFormat) { 
530 			if(!(typeof PartnerConfig!="undefined" && PartnerConfig.getKey("use_12Hour_format",true) || is12HourFormat)) return timeString;
531 
532 			var a =  timeString.split(" ");
533 			if(a.length < 2) return timeString;
534 			var amPm = a[1];
535 			var t = a[0];
536 			var tA = t.split(":");
537 			if(amPm.toLowerCase() == "am") {
538 				var h =this.parseInt(tA[0]);
539 				if(h<12)
540 					return t;
541 				else  {
542 					var rt = "00";
543 					for(var i=1; i<tA.length;i++)
544 						rt = rt + ":"+tA[i];
545 					Platform.log("returning value"+rt);
546 					return rt;
547 				}
548 			}
549 			else { 
550 			   var h =this.parseInt(tA[0]);
551 			   if(h!=12) {
552 				h = h+12;
553 				tA[0] = h;
554 				return tA.join(":");
555 			   } else 
556 				return t;
557 			 }
558 			
559 	},
560 	matchDate : function(date1,date2,matches)
561 	{
562 		var match =false;
563 		var m = (this.parseInt(matches[2])-1);
564 		var d = this.parseInt(matches[3]);
565 		var y = matches[1];
566 		if(date1 && date1!="Invalid Date") { //if format yyyy/mm/dd works
567 			if( (date1.getMonth() == m || date1.getMonth() == (m+1)) && (date1.getDate() == d || date1.getDate()==(d-1) || date1.getDate()==(d+1) ) && (date1.getFullYear()==y) ) 
568 				return true;
569 			else return false;
570 		}
571 		else if(date2 && date2!="Invalid Date") { //if format yyyy-mm-dd works
572                         if( (date2.getMonth() == m || date2.getMonth() == (m+1)) && (date2.getDate() == d || date2.getDate()==(d-1) || date2.getDate()==(d+1)) && (date2.getFullYear()==y) )
573                                 return true;
574                         else return false;
575                 }
576 		else return false;
577 
578 
579 	},
580 	isValidDate : function(string,format) {
581 	var reg = "";
582 	if(format && format!="")
583 	{
584 		if(format=="yyyy-mm-dd" || format=="YYYY-MM-DD")
585 			reg ="^([0-9]{4})-([0-9]{2})-([0-9]{2})$";
586 		else if( format=="YYYY/MM/DD" || format=="yyyy/mm/dd")
587 			reg ="^([0-9]{4})/([0-9]{2})/([0-9]{2})$";
588 		else if(format=="dd-mm-yyyy" || format=="DD-MM-YYYY")
589 			reg ="^([0-9]{2})-([0-9]{2})-([0-9]{4})$";
590 		else if(format=="DD/MM/YYYY" || format=="dd/mm/yyyy")
591 			 reg ="^([0-9]{2})/([0-9]{2})/([0-9]{4})$";
592 	}
593 	else reg ="^([0-9]{4})-([0-9]{2})-([0-9]{2})$";
594 	var IsoDateRe = new RegExp(reg);
595 	string = string.trim();  
596 	var matches = IsoDateRe.exec(string);
597 	if (!matches) return false;
598  	var composedDate = new Date(matches[0].split("-").join("/"));
599 	var composedDate1 = new Date(matches[0]);
600 	var result =this.matchDate(composedDate,composedDate1,matches);
601 	//if(composedDate1 && composedDate1!="Invalid Date") composedDate = new Date(matches[0]);//work around  - create date object using yyy-mm-dd format if works
602 	 //if( (composedDate.getMonth() == (this.parseInt(matches[2])-1)) && (composedDate.getDate() == matches[3]) &&(composedDate.getFullYear() == matches[1]) )
603 		return result;
604 	//else return false;
605 
606 	},
607 	setEpochOffset : function(seconds)
608 	{
609 		var currMillis = new Date().getTime();
610 		var clsMillis = seconds*1000;
611 		this.epochOffset = currMillis - clsMillis;
612 	},
613 	getNow : function()
614 	{
615 		var dMillis =  new Date().getTime();
616 		var dCurr = dMillis - this.epochOffset;
617 		return dCurr;
618 	},
619 	parseInt : function(str)
620 	{ if(s==0 || s=="0") return parseInt(s);
621 
622 		var s = (str+"").trim();
623 		while(s.indexOf('0') == 0 ) //start with 0.leave firs zero
624 		{
625 			s = s.substr(1,s.length-1);
626 		}
627 		if(str && str!="" && s=="") s = "0";
628 		return parseInt(s);
629 	}
630 	
631 };
632 DateTimeUtil.addProtoToDate();
633