Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple date object question 2

Status
Not open for further replies.

786snow

Programmer
Nov 12, 2006
75
what this Date object suppose to have , when I alert , it gives me wrong month, gives me July instead of June.



<script>


birthday = new Date(2007,06,03)
alert(birthday);


</script>
 
Months are 0-indexed in javascript, so if you're not writing it as a string then you need to subtract 1 month from each date you set that way.

On a side note, I looked at your profile and you've asked 30+ questions on this forum and never awarded a star to anybody that's helped you. It is a common courtesy to click the "thank xxx for this valuable post" whenever someone provides you an accurate answer that helped you out (this is what structures the MVP system on this site). Normally I do not point this out in threads where I answer questions, but after being a member here for the period that you have this is something you should be aware of.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
One thing that's come up in this forum a few times is problems from using a zero in front of a number like the OP did in the example. In Javascript, any number starting with a zero is interpreted as an octal number. If you use 08 or 09, your code will produce an error because 8 and 9 are not valid octal digits.

Lee
 
Hi, Thanks i did not know about the MVP sysytem, from now on I will. Infact I already gave both you guys star.



I am pasting my code, their is wierd stuff you will notice if you run it, if you pick your date from the calander and click on submit button, I break
that into year , month and day and then alert to make sure. but when I create date object and print it gives me something totally different, i don't know what is going on.

************************************







<SCRIPT LANGUAGE="JavaScript">
function show_calendar(str_target, str_datetime) {


var arr_months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
var n_weekstart = 1; // day week starts from (normally 0 or 1)

var dt_datetime = (str_datetime == null || str_datetime =="" ? new Date() : str2dt(str_datetime));

var dt_prev_month = new Date(dt_datetime);
dt_prev_month.setMonth(dt_datetime.getMonth()-1);
var dt_next_month = new Date(dt_datetime);
dt_next_month.setMonth(dt_datetime.getMonth()+1);
var dt_firstday = new Date(dt_datetime);
dt_firstday.setDate(1);
dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
var dt_lastday = new Date(dt_next_month);
dt_lastday.setDate(0);


// print calendar header
var str_buffer = new String (
"<html>\n"+
"<head>\n"+
" <title>Calendar</title>\n"+
"</head>\n"+
"<body bgcolor=\"White\">\n"+
"<table cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
"<tr><td bgcolor=\"#768190\">\n"+
"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
"<tr>\n <td bgcolor=\"#768190\"><a href=\"javascript:window.opener.show_calendar('"+
str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\" style=\"color:#ffffff;text-decoration:none\">"+

"&lt;&lt;</a></td>\n"+
" <td bgcolor=\"#768190\" colspan=\"5\" align=center>"+
"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
" <td bgcolor=\"#768190\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
+str_target+"', '"+dt2dtstr(dt_next_month)+"');\" style=\"color:#ffffff;text-decoration:none\">"+

"&gt;&gt;</a></td>\n</tr>\n"
);

var dt_current_day = new Date(dt_firstday);
// print weekdays titles
str_buffer += "<tr>\n";
for (var n=0; n<7; n++)
str_buffer += " <td bgcolor=\"#E3E3E3\">"+
"<font color=\"#000000\" face=\"tahoma, verdana\" size=\"2\">"+
week_days[(n_weekstart+n)%7]+"</font></td>\n";
// print calendar table
str_buffer += "</tr>\n";
while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
dt_current_day.getMonth() == dt_firstday.getMonth()) {
// print row heder
str_buffer += "<tr>\n";
for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
if (dt_current_day.getDate() == dt_datetime.getDate() &&
dt_current_day.getMonth() == dt_datetime.getMonth())
// print current date
str_buffer += " <td bgcolor=\"#cccccc\" align=\"right\">";
else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
// weekend days
str_buffer += " <td bgcolor=\"#ebeef5\" align=\"right\">";
else
// print working days of current month
str_buffer += " <td bgcolor=\"white\" align=\"right\">";

if (dt_current_day.getMonth() == dt_datetime.getMonth())
// print days of current month
str_buffer += "<a href=\"javascript:window.opener."+str_target+
".value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
else
// print days of other months
str_buffer += "<a href=\"javascript:window.opener."+str_target+
".value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
dt_current_day.setDate(dt_current_day.getDate()+1);
}
// print row footer
str_buffer += "</tr>\n";
}
// print calendar footer
str_buffer +=

"</table>\n" +
"</tr>\n</td>\n</table>\n" +
"</body>\n" +
"</html>\n";

var vWinCal = window.open("", "Calendar",
"width=200,height=200,status=no,resizable=yes,top=200,left=600");
vWinCal.opener = self;
var calc_doc = vWinCal.document;
calc_doc.write (str_buffer);
calc_doc.close();
}
// datetime parsing and formatting routimes.
function str2dt (str_datetime) {
var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
//if (!re_date.exec(str_datetime))
//return alert("Invalid Datetime format: "+ str_datetime);
if (!re_date.exec(str_datetime))
return (new Date());
// return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2));
}
function dt2dtstr (dt_datetime) {
return (new String (
//dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getFullYear()+" "));
(dt_datetime.getMonth()+1)+"/"+dt_datetime.getDate()+"/"+dt_datetime.getFullYear()));
}




function func_submitRequest() {


var fromDate= document.getElementById('fromDate').value;
var toDate= document.getElementById('toDate').value;



var ComparsionFlag= fun_compareDates(fromDate, toDate);








}

function fun_compareDates(fromDate, toDate){

var startDate=fun_getDateObject(fromDate,"/");

alert(startDate);


}


function fun_getDateObject(dateString,dateSeperator)
{
//This function return a date object after accepting
//a date string ans dateseparator as arguments
var curValue=dateString;
var sepChar=dateSeperator;
var curPos=0;
var cDate,cMonth,cYear;

//extract day portion
curPos=dateString.indexOf(sepChar);
cDate=dateString.substring(0,curPos);

//extract month portion
endPos=dateString.indexOf(sepChar,curPos+1);
cMonth=dateString.substring(curPos+1,endPos);

//extract year portion
curPos=endPos;
endPos=curPos+5;
cYear=curValue.substring(curPos+1,endPos);

//Create Date Object

alert(cYear);
alert(cMonth);
alert(cDate);

dtObject=new Date(cYear,cMonth,cDate);
return dtObject;
}









</script>




<HTML>
<BODY>
<FORM ACTION="/NASApp/EDocsCtx/EDocsServlet?actionType=2" METHOD="POST" NAME= "Statements" onsubmit="return func_submitRequest()">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" nowrap><input type="radio" name="date_type" value="date_range" ><b>Date Range:</b></input></td>
<td align="center"><input type="text" style="FONT-SIZE: 10px" name="fromDate" id="fromDate" size=10 maxlength=10 value=>&nbsp;<a href="javascript:show_calendar('document.Statements.fromDate', '');"><img SRC=" ALIGN="absmiddle" BORDER=0 ></a></td>
<td align="center"><b>to</b></td>
<td align="right"><input type="text" style="FONT-SIZE: 10px" name="toDate" id="toDate" size=10 maxlength=10 value=>&nbsp;<a href="javascript:show_calendar('document.Statements.toDate', '');"><img SRC=" ALIGN="absmiddle" BORDER=0 ></a></td>
<td align="right"> <input name="edocsStatements" type="image" ALT="Search" SRC="/images/b_Search.gif" BORDER="0" HEIGHT="21" WIDTH="68" /> </td>
</tr>
</table>
</BODY>
</HTML>
 
You know... the Date object is smart enough to parse a string date in the format that you have supplied. You don't have to break it down like you are doing. For example, change this function to the following:
Code:
function fun_compareDates(fromDate, toDate){
    var startDate = new Date(fromDate);
    alert(startDate);
    var stopDate = new Date(toDate);
    alert(stopDate);
}

You'll see that the Date objects reflect the days that you're expecting.

Besides.... using your previous method, what happens if the users type the date into the textboxes manually and put it in a different format, like 2007/05/23. Your original function expected the dates to be formatted a certain way, so someone hand-typing a date into the box in a different format would crash the date. Fortunately the Date object is smart enough to parse dates in a bunch of different formats. You might want to play with it a little though to see what sort of things will break it. I know for a fact that it will not parse dates as SQL returns them (i.e. 2007-05-23)


-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
thanks a lot again, it worked great
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top