Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

return date giving extra minute

Posted on 2011-05-10
6
Medium Priority
?
321 Views
Last Modified: 2012-05-11
I am working on creating a schedule function:
code attached below

scenario
dates passed in log
2011-05-01 00:00:00.0
2011-05-31 23:59:00.0
utility used
2011-05-31 23:59:00.0

dates being reported as end date in report (as is sent to database)
2011-06-01 12:00:00.0 am

issue: When dates are passed via the bls using autodate settings 1-8 the code is automatically adding the next minute - therefor reporting the requested day as the next day midnight.
function setStartEndDate(){
			// 2011/01/27 09:06 am
			var d = new Date();

			var currHour = d.getHours();
			ampm = (currHour < 12) ? "AM" : "PM";
			currHour = (currHour == 0) ? 12 : currHour;
			currHour = (currHour > 12) ? currHour - 12 : currHour;
			var currMin = d.getMinutes() + "";
			currMin = (currMin.length == 1) ? "0"+currMin : currMin;
	
			// if startdate is empty and custom dates being used populate start date
			if(milsoftElement(form + ":startDate").value <= 0 && milsoftElement(form + ":commonDates").value == "9"){
				// 1970/01/01 12:01 AM
				milsoftElement(form + ":startDate").value = "1970/01/01 12:01 am";
				//milsoftElement(form + ":startDate").value = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + (d.getDate()-1) + " " + currHour + ":" + currMin + " " + ampm;
			}
			
			// if enddate is empty and custom dates being used populate end date
			if(milsoftElement(form + ":endDate").value <= 0 && milsoftElement(form + ":commonDates").value == "9"){
				// 2040/01/01 11:59 PM
				milsoftElement(form + ":endDate").value = "2040/01/01 11:59 pm";
				//milsoftElement(form + ":endDate").value = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " " + currHour + ":" + currMin + " " + ampm;
			}
			return true;
		}

Open in new window

0
Comment
Question by:Tagom
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35733309
Maybe you can put some printout (say, alert window)
with d.getMinutes() - does it consistently report one minute more?

But
new Date()
gives you current date so you just see that it gives one minute more than the system clock?
0
 

Author Comment

by:Tagom
ID: 35733646
opps, attached wrong code...
The code below is what calculates our autodates
switch(parseInt(code)) {
                  case 1: //today
                  break;
                  case 2: //yesterday
                        start.setDate(start.getDate() - 1);
                        end.setDate(end.getDate() - 1);
                  break;
                  case 3: //this week
                        start.setDate(start.getDate() - start.getDay());
                        end.setDate(start.getDate() + 6);
                  break;
                  case 4: //last week
                        start.setDate(start.getDate() - start.getDay() - 7);
                        end.setDate(end.getDate() - end.getDay() - 1);                                    
                  break;
                  case 5: //this month
                        start.setDate(1);
                        end.setMonth(end.getMonth() + 1);
                        end.setDate(0);
                  break;
                  case 6: //last month
                        start.setMonth(start.getMonth() - 1);
                        start.setDate(1);
                        end.setDate(0);
                  break;
                  case 7: //this year
                        start.setMonth(0);
                        start.setDate(1);
                        end.setMonth(11);
                        end.setDate(31);                                    
                  break;
                  case 8: //last year
                        start.setMonth(0);
                        start.setDate(1);
                        start.setFullYear(start.getFullYear() - 1);
                        end.setMonth(11);
                        end.setDate(31);                                    
                        end.setFullYear(start.getFullYear());
                  break;
                  //9: custom

for some reason unless I use case 9 the return values or what is being passed to the database is midnight of the next day.

/*
	REQUIRES:
		1) the following references:
			<link rel="stylesheet" type="text/css" href="/IVRstatic/css/calendar/calendar-blue2.css" />
			<link rel="stylesheet" type="text/css" href="/IVRstatic/css/wui.css">
			
			<script language="javaScript" src="/IVRstatic/jsBin/musCommon.js"></script>		
			<script language="javascript" src="/IVRstatic/jsBin/calendar/calendar.js"></script>
			<script language="javascript" src="/IVRstatic/jsBin/calendar/lang/calendar-en.js"></script>
			<script language="javascript" src="/IVRstatic/jsBin/calendar/calendar-setup.js"></script>
			<script language="javascript" src="/IVRstatic/jsBin/musDateFilter.js"></script>

		2) the following JSF - update with proper bean references, and update javascript: [form = "dateFilter"] to have correct form name
			<h:panelGrid columns="1" styleClass="valignTop" rowClasses="appLogin">
					<h:outputText value="#{labels.commonDates}" />	
					<h:selectOneMenu id="commonDates" onchange="changeDates(this.value)" >
						<f:selectItem id="allDates" itemLabel="#{labels.allDates}" itemValue="0" />
						<f:selectItem id="today" itemLabel="#{labels.today}" itemValue="1" />
						<f:selectItem id="yesterday" itemLabel="#{labels.yesterday}" itemValue="2" />
						<f:selectItem id="thisWeek" itemLabel="#{labels.thisWeek}" itemValue="3" />
						<f:selectItem id="lastWeek" itemLabel="#{labels.lastWeek}" itemValue="4" />
						<f:selectItem id="thisMonth" itemLabel="#{labels.thisMonth}" itemValue="5" />
						<f:selectItem id="lastMonth" itemLabel="#{labels.lastMonth}" itemValue="6" />
						<f:selectItem id="thisYear" itemLabel="#{labels.thisYear}" itemValue="7" />
						<f:selectItem id="lastYear" itemLabel="#{labels.lastYear}" itemValue="8" />
						<f:selectItem id="customDates" itemLabel="#{labels.customDates}" itemValue="9" />
					</h:selectOneMenu>
					<h:panelGroup>
						<h:selectBooleanCheckbox id="startFilter" value="#{dtfi.filterStart}" onchange="disableDate('start', !this.checked)" />
						<h:outputText value="#{labels.startDate}" />
					</h:panelGroup>
					<h:panelGroup>
						<h:inputText value="" id="startDate" readonly='true' onchange="dateChanged()" disabled="#{dtfi.filterStart == false}" />
						<h:inputHidden id="startMillis" value="#{dtfi.startMillis}" />
						<h:commandButton id="startCal" type="button" value="..." disabled="#{dtfi.filterStart == false}" />					
					</h:panelGroup>	
					<h:panelGroup>
						<h:selectBooleanCheckbox id="endFilter" value="#{dtfi.filterEnd}" onchange="disableDate('end', !this.checked)" />
						<h:outputText value="#{labels.endDate}" />
					</h:panelGroup>
					<h:panelGroup>
						<h:inputText value="" id="endDate" readonly='true' onchange="dateChanged()" disabled="#{dtfi.filterEnd == false}" />
						<h:inputHidden id="endMillis" value="#{dtfi.endMillis}" />
						<h:commandButton id="endCal" type="button" value="..." disabled="#{dtfi.filterEnd == false}" />
					</h:panelGroup>
					<h:commandButton id="applyFilters" value="OK" action="#{dtfi.applyFilters}" type="submit" />
					<h:outputText value="#{dtfi.alert}" />	
					<h:inputHidden id="userInput" value="#{dtfi.userInput}" />
			</h:panelGrid>		
			<script language="javaScript">
				form = "dateFilter";
				initDateFilter();
			</script>

*/

	var autoChange;
	var Calendar_startDate;
	var Calendar_endDate;
	var calFormat;
	function initDateFilter(format, timeToo) {
		calFormat = format;
		autoChange = false;
		if (parseInt(sampleElement(form + ":commonDates").value) == 9) {				
			showDates(new Date(parseInt(sampleElement(form + ":startMillis").value)), new Date(parseInt(sampleElement(form + ":endMillis").value)));
		}
		changeDates(sampleElement(form + ":commonDates").value);				
		Calendar_startDate = Calendar.setup({
			inputField	: form + ":startDate",  
			ifFormat	: format,
			showsTime	: timeToo,
			timeFormat	: "12",
			button		: form + ":startCal",
			step		: 1,   
			onUpdate	: updateStartMillis
		});
		Calendar_endDate = Calendar.setup({
			inputField	: form + ":endDate",
			ifFormat	: format,
			showsTime	: timeToo,
			timeFormat	: "12",
			button		: form + ":endCal",   
			step		: 1,                
			onUpdate	: updateEndMillis
		});
	}
	function updateStartMillis(cal) {
		sampleElement(form + ":startMillis").value = cal.date.getTime();
	}
	function updateEndMillis(cal) {
		sampleElement(form + ":endMillis").value = cal.date.getTime();
	}
	function showDates(start, end) {
		autoChange = true;

		sampleElement(form + ":startDate").value = start.print(calFormat);
		sampleElement(form + ":endDate").value = end.print(calFormat);
		sampleElement(form + ":startMillis").value = start.getTime();
		sampleElement(form + ":endMillis").value = end.getTime();
		updateUserInput();
		
		autoChange = false;
	}
	function updateUserInput() {
		var msg = sampleElement(form + ":commonDates").options[sampleElement(form + ":commonDates").selectedIndex].text;
		if (sampleElement(form + ":startFilter").checked) 
			msg = msg + " [" + sampleElement(form + ":startDate").value + " - ";
		else 
			msg = msg + " [* - ";
		if (sampleElement(form + ":endFilter").checked) 
			msg = msg + sampleElement(form + ":endDate").value + "]";
		else
			msg = msg + "*]";
		sampleElement(form + ":userInput").value = msg;			    
	}
	
	function dateChanged(cntl) {
	
		if (cntl) {
			try {
				var d = Date.parse(cntl.value);
				if (d && !isNaN(d) && (d > 0)) {
					d = new Date(d);
					cntl.value = d.print(calFormat);
				} else { 
					dateParseError(true, cntl, "Invalid date format");
					return false;
				}
			} catch (e) {
				dateParseError(true, cntl, "Invalid date format - "+ e);
				return false;
			}
			
			dateParseError(false, cntl);
		}
		
		if (!autoChange) {
			sampleElement(form + ":commonDates").value = 9;
			updateUserInput();
		}
		if(!sampleElement(form + ":startFilter").checked)
			sampleElement(form + ":startMillis").value = "0";			
		else if(sampleElement(form + ":endDate").value.length > 0)
			sampleElement(form + ":startMillis").value = Date.parse(sampleElement(form + ":startDate").value);	
		else
			sampleElement(form + ":startMillis").value = "0";
				
		if(!sampleElement(form + ":endFilter").checked)
			sampleElement(form + ":endMillis").value = "2209010400000";				
		else if(sampleElement(form + ":endDate").value.length > 0)
			sampleElement(form + ":endMillis").value = Date.parse(sampleElement(form + ":endDate").value);
		else
			sampleElement(form + ":endMillis").value = "2209010400000";
	}

	function dateParseError(errState, cntl, msg) {

		if (errState) {
			
			if (!sampleElement(cntl.id + "_err")) {
				var mSpan = document.createElement("span");
				mSpan.setAttribute("id", cntl.id + "_err");
				mSpan.setAttribute("class","validationErr");
				if (typeof mSpan.textContent != "undefined") mSpan.textContent = msg;
				else mSpan.innerText = msg;
				cntl.parentNode.appendChild(mSpan);
				cntl.style.border="3px inset #f00";
			}
			
			var ad = sampleElement(form+":applyDates");
			if (ad) {
				if (typeof ad.dateError == "undefined") ad.dateError = 1;
				else ad.dateError++;
				ad.disabled = true;
			}

			if (cntl.id == form+":startDate") {
				var sc = sampleElement(form+":startCal");
				if (sc) sc.onclick();
			} else if (cntl.id == form+":endDate") {
				var ec = sampleElement(form+":endCal");
				if (ec) ec.onclick();
			}
		
		} else { 
			var mSpan = sampleElement(cntl.id + "_err");
			if (mSpan) mSpan.parentNode.removeChild(mSpan);
			cntl.style.border="1px inset #999";
			
			var ad = sampleElement(form+":applyDates");
			if (ad) {
				if (typeof ad.dateError == "undefined") ad.dateError = 0;
				else ad.dateError--;
				if (ad.dateError == 0) ad.disabled = false;
			}
		}
	
	}

	
	function disableDate(prefix, disabled) {
		sampleElement(form + ":" + prefix + 'Date').disabled = disabled;
		sampleElement(form + ":" + prefix + 'Cal').disabled = disabled;
		sampleElement(form + ":" + prefix + 'Filter').checked = !disabled;
		if (!autoChange) {
			sampleElement(form + ":commonDates").value = 9;
		}
		updateUserInput();
	}
	function changeDates(code) {
		if (parseInt(code) == 9){
			return;
		}
		if (parseInt(code) == 0) {
			autoChange = true;
			disableDate("start", true);
			disableDate("end", true);
			autoChange = false;
			return;
		}
		
		autoChange = true;
		disableDate("start", false);
		disableDate("end", false);
		autoChange = false;
		
		start = new Date();
		
		end = new Date();
		if (code < 10) {
			start.setHours(0);
			start.setMinutes(0);
			start.setSeconds(0);
			start.setMilliseconds(0);
			end.setHours(23);
			end.setMinutes(59);
			end.setSeconds(59);
			end.setMilliseconds(999);
		}
		switch(parseInt(code)) {
			case 1: //today
			break;
			case 2: //yesterday
				start.setDate(start.getDate() - 1);
				end.setDate(end.getDate() - 1);
			break;
			case 3: //this week
				start.setDate(start.getDate() - start.getDay());
				end.setDate(start.getDate() + 6);
			break;
			case 4: //last week
				start.setDate(start.getDate() - start.getDay() - 7);
				end.setDate(end.getDate() - end.getDay() - 1);						
			break;
			case 5: //this month
				start.setDate(1);
				end.setMonth(end.getMonth() + 1);
				end.setDate(0);
			break;
			case 6: //last month
				start.setMonth(start.getMonth() - 1);
				start.setDate(1);
				end.setDate(0);
			break;
			case 7: //this year
				start.setMonth(0);
				start.setDate(1);
				end.setMonth(11);
				end.setDate(31);						
			break; 
			case 8: //last year
				start.setMonth(0);
				start.setDate(1);
				start.setFullYear(start.getFullYear() - 1);
				end.setMonth(11);
				end.setDate(31);						
				end.setFullYear(start.getFullYear());
			break;
			//9: custom
			case 10://past hour
				start.setHours(start.getHours() - 1);
			break;
			case 11://past 2 hours
				start.setHours(start.getHours() - 2);
			break;
			case 12://past 4 hours
				start.setHours(start.getHours() - 4);
			break;
			case 13://past 6 hours
				start.setHours(start.getHours() - 6);
			break;
			case 14://past 8 hours
				start.setHours(start.getHours() - 8);
			break;
			case 15://past 12 hours
				start.setHours(start.getHours() - 12);
			break;
		}
		autoChange = true;
		showDates(start, end);
		autoChange = false;
	}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35733706
>                         end.setDate(0);

that doesn't look right, date starts at 1
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:msk_apk
ID: 35738556
I believe this is not the right approach. For example, Date.getDate() returns the date between 1 - 31 of that month. Hence when you do date.getDate()-1, might give problems when dayOfMonth = 1. So in that case best method or parameter to be used ot dayOfYear property in GregorialCalendar class.
Also it would be error free if you use Calendar.JANUARY etc. For setting the week WEEK_OF_YEAR
constant can be used. So this code has to be changed to use GregorianCalendar instead of Date. Usage of GregorianCalendar would resolve many issues.
0
 

Accepted Solution

by:
Tagom earned 0 total points
ID: 35742153
@objects - this was done correctly - it works on all pages except the schedules report.
@msk apk -  do not have the ability to change to gregorianCalendar - however it would be much better!

SOLUTION - I changed the milliseconds from 999 to 0 and it now pulls the correct time!
0
 

Author Closing Comment

by:Tagom
ID: 35775434
This solution fixed my issue
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question