homepage Welcome to WebmasterWorld Guest from 54.167.238.60
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript - checking dates
How can I make January output as 1 instead of 0 using this script.
lasko




msg:1483312
 11:28 am on Jun 21, 2003 (gmt 0)

I hope someone can help me modify this script, It is exactly what I require really perfect the only trouble is the output in the form results produces a 0 for january and a 1 for february and so on.

I know javascript reads from 0 to 11 for months but I need to make it user friendly please can someone help I have tried everything i know.

Sorry if this is the wrong place to post I couldn't find any Javascript catergory.

---------

<html>
<head>
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var begin_year = 2000
var year_count = 100

var begin_today = true // true = the start day must be bigger than the day of today
var min_days = 0 // the mimum days between the start day and enddate
var max_days = false // the maximum days between the start day and enddate
//Set this value on (max_days = false) for unlimited days
var set_on_today = true // set the startday on today
var end_start = 8 // only importent when you set the set_on_day on true, the value of the end date is the date of today + end_start

var bday,bmon,byear,eday,emon,eyear
var todayd = new Date()

function init(form)
{
bday = form.bday;
bmon = form.bmon;
byear = form.byear;
eday = form.eday;
emon = form.emon;
eyear = form.eyear;
if(set_on_today == true)
{
bday.options[(todayd.getDate()-1)].selected = true;
bmon.options[todayd.getMonth()].selected = true;
byear.options[(todayd.getFullYear() - begin_year)].selected = true;
milli = -(0 - todayd) + (1000*60*60*24*end_start)
var todayde = new Date(milli)
eday.options[(todayde.getDate()-1)].selected = true;
emon.options[todayde.getMonth()].selected = true;
eyear.options[(todayde.getFullYear() - begin_year)].selected = true;
}
}

function value_date(cday,cmon,cyear)
{
tempdate = new Date(cyear.value,cmon.value,cday.value)
if((cday.value == tempdate.getDate()) && (cmon.value == tempdate.getMonth()) && (cyear.value == tempdate.getFullYear())) return true
return false
}

function check()
{
if(!value_date(bday,bmon,byear))
{
alert("The start date is an invalid date!\nPlease reselect.");
return false
}
if(!value_date(eday,emon,eyear))
{
alert("The end date is an invalid date!\nPlease reselect.");
return false
}
if(begin_today == true)
{
var tempdate = new Date(byear.value,bmon.value,bday.value);
var tempdatet = new Date(todayd.getFullYear(),todayd.getMonth(),todayd.getDate());
if(tempdatet > tempdate)
{
alert("The start date must bigger than the date of today!");
return false
}
}
var tempdatee = new Date(eyear.value,emon.value,eday.value);
if(tempdate > tempdatee)
{
alert("The end date must be bigger than the start date!");
return false
}
one_day = 1000*24*60*60
days_between = (tempdatee - tempdate)/one_day

if(days_between < min_days)
{
alert("The minimum days between is "+ min_days + " days!");
return false
}
if(max_days!= false)
{
if(days_between > max_days)
{
alert("The maximum days between is "+ max_days + " days!");
return false
}}
}

</script>
</head>
<body>
<FORM action="" method = "post" name = "form" onsubmit = "return check(this)" >
<!-- Below are the date boxes and script to be placed anywhere in form -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Start </td>
<td colspan="2"><select name=bday size=1 class=selecter>
<script language="JavaScript">
for(i=1;i<=31;i++) document.write("<option value="+ i + ">"+ i);
</script>
</select> <select name=bmon size=1 class=selecter>
<script language="JavaScript">
for(i=0;i<12;i++) document.write("<option value="+ i + ">"+ months[i]);
</script>
</select> <select name=byear size=1 class=selecter>
<script language="JavaScript">
for(i=0;i<year_count;i++) document.write("<option value="+ (i+begin_year) + ">"+ (i+begin_year));
</script>
</select></td>
</tr>
<tr>
<td>End
</td>
<td><select name=eday size=1 class=selecter>
<script language="JavaScript">
for(i=1;i<=31;i++) document.write("<option value="+ i + ">"+ i);
</script>
</select> <select name=emon size=1 class=selecter>
<script language="JavaScript">
for(i=0;i<12;i++) document.write("<option value="+ i + ">"+ months[i]);
</script>
</select> <select name=eyear size=1 class=selecter>
<script language="JavaScript">
for(i=0;i<year_count;i++) document.write("<option value="+ (i+begin_year) + ">"+ (i+begin_year));
init(document.form)
</script>
</select></td>
</tr>
</table>
<td> <br>
<input name="" type="submit">
</form></body></html>

 

RonPK




msg:1483313
 11:48 am on Jun 21, 2003 (gmt 0)

Hi Lasko,

The script in the select boxes 'bmon' and 'emon' now reads like this:

for(i=0;i<12;i++) document.write("<option value="+ i + ">"+ months[i]);

Change that into this:

for(i=1;i<=12;i++) document.write("<option value="+ i + ">"+ months[i-1]);

I hope that solves your problem.

lasko




msg:1483314
 12:07 pm on Jun 21, 2003 (gmt 0)

Thanks for that it has solved the output but now the form
thinks May is the current month and not June.

For example

if I submit 20th June 2003 it sends perfectly

However

if I submit 20th May 2003 its warns me the start date should be more then today.

Now how can I get that to work for June and not May.

Javascript reads it that May is the current month being the value of 5.

Any thoughts

RonPK




msg:1483315
 1:27 pm on Jun 21, 2003 (gmt 0)

I see. Could you perhaps explain what's so user unfriendly about posting a 0 for January? And, instead of hacking the javascript, wouldn't it be much easier to do something like 'month = month + 1' in the receiving script?

lasko




msg:1483316
 2:38 pm on Jun 21, 2003 (gmt 0)

The script is going to be used for many property owners advertising holidays etc. I will also be using it for some other web sites for holiday related enquiries.

The results end up being 0 instead of 1 for january etc. which is very confusing, specially when it will be received by people who have only just discovered the power of email.

I am not really a programmer but I have been able to manipulate one or two php scripts and javascripts just to personalise it.

I have searched the internet and asked around for another version of this form but with no success, all their is to offer are pop up calander pickers and date checkers that check manually inputed dates which is again not very user friendly.

Their must be a solution some how, I have tried moving the months down one for instance putting ("","january","february",".....) but again the javascript will still only recognise 5 as being May.

Its so annoying and I have been very close to cracking it
but theirs always something not right.

I am very gratefull for your help and advice thanks.

RonPK




msg:1483317
 4:18 pm on Jun 21, 2003 (gmt 0)

Let's try this:
1) modify the function value_date():

function value_date(cday,cmon,cyear)
{
tempdate = new Date(cyear.value,cmon.value-1,cday.value)
if((cday.value == tempdate.getDate()) && (cmon.value-1 == tempdate.getMonth()) && (cyear.value == tempdate.getFullYear())) return true
return false
}

2) some changes in the function check():

var tempdate = new Date(byear.value,bmon.value-1,bday.value);

and

var tempdatee = new Date(eyear.value,emon.value-1,eday.value);

3) the script in the select boxes for the months:

for(i=0;i<12;i++) document.write("<option value="+ (i+1) + ">"+ months[i]);

This should pass the validation and post human readable month numbers.

lasko




msg:1483318
 5:00 pm on Jun 21, 2003 (gmt 0)

Great, works very well.

I had actually made one or two changes that you had just suggested however I did not change the settings at the top of the script.

Thank you very much for your help really appreciated.

Cheers

g1smd




msg:1483319
 10:30 am on Jun 22, 2003 (gmt 0)

Some more comments about internationally accepted date formats in an internet context: [webmasterworld.com...]

RonPK




msg:1483320
 12:57 pm on Jun 22, 2003 (gmt 0)

Some more comments [..]

LOL!

Anyway, maybe we should form a posse to round up the people who decided to start at 0 when counting months...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved