Jump to content

Help in Date Format


lilywong

Recommended Posts

I have four textboxes, (txtDate1,txtDate2,txtDate3,txtDate4), when i key in in txtDate1, for example, 11/05/2006, the rest will automatically become 12/05/2006,13/05/2006,14/05/2006. however, if 14/05/2006 is sunday, it will need to skip that and become 15/05/2006. i need a function to differentiate whether the date is sunday, if it is, that need to be skipped.

and the date format in (dd/mm/yyyy).In this case for example,
1/1/2006 (friday)
2/1/2006 (saturday)
3/1/2006 (sunday)
4/1/2006 (monday)
5/1/2006 (tuesday)

so the 3/1/2006 will be skipped.
so txtDate1 will be 1/1/2006, txtDate2 will be 2/1/2006 , txtDate3 will be 4/1/2006 , txtDate4 will be 5/1/2006 .

Just consider the user key in dd/mm/yyyy. (11/05/2006), but NOT mm/dd/yyyy

thanks you so much, need help urgently !~!~
thank you so much !
Link to comment
Share on other sites

  • 2 weeks later...
Hi there,,

That one was funny to script,,, :)
Try it,,
[code]<html>
<head>
<title>Dating,,</title>
<style>
.datebox{
    border: 1px solid gray;
    width:80px;
    background-color:#FAF9F0;
}
</style>
</head>
<body>
Choose a date (format:dd/mm/yyyy) <input type="textbox" id="date1" class="datebox" onchange="populate_dates();"><br>
1 day after: <div id="date2" class="datebox" > </div>
2 days after: <div id="date3" class="datebox"  > </div>
3 days after: <div id="date4" class="datebox" > </div>
with Sundays skipped :)
<script>

var dateParsePatterns = [
    // dd/mm/yyyy
    {   re: /(\d{1,2})\/(\d{1,2})\/(\d{4})/,
        handler: function(bits) {
            var d = new Date();
            d.setDate(parseInt(bits[1]));
            d.setYear(bits[3]);

    if ((parseInt(bits[2]) == 0) || (parseInt(bits[2]) > 12) || (parseInt(bits[1]) == 0) || (parseInt(bits[1]) > 31))
        {
        d="Invalid date";
        }
    else
        {
                d.setMonth(parseInt(bits[2])-1); // Because months indexed from 0
        }
            return d;
        }
    },
];

function parseDateString(s){
        var re = dateParsePatterns[0].re;
        var handler = dateParsePatterns[0].handler;
        var bits = re.execu(s);
        if (bits) {
            return handler(bits);
        }
return "Invalid date";
}
function populate_dates()
{
var date1 = document.getElementById('date1').value;
control=parseDateString(date1);

if (control=="Invalid date")
    {
    document.getElementById('date2').innerHTML=control;
    document.getElementById('date3').innerHTML=control;
    document.getElementById('date4').innerHTML=control;
    }
else
    {
    print_next_dates(control)
    var temps = control;
    }
}

function print_next_dates(temps)
{
for (i=2;i<5;i++)
    {
    temps.setDate(temps.getDate() + 1); // adding 1 day to the date
    if (temps.getDay() == 0) // skipping the sundays (cause 0 means sunday, using the getDay function)
        {
        temps.setDate(temps.getDate() + 1);
        }
    var nextday = temps.getDate(); // getting the day (05/12/2005 gives 5)
    var nextmonth = temps.getMonth()+1; // getting the month (05/12/2005 gives 12)
    var nextyear = temps.getFullYear(); // getting the full year (05/12/2005 gives 2005)
    if (nextday<10)
        {
        nextday = "0"+nextday; // adding a '0' before the single number
        }
    if (nextmonth<10)
        {
        nextmonth = "0"+nextmonth; // adding a '0' before the single number
        }
    eval("document.getElementById('date"+i+"').innerHTML='"+nextday+"/"+nextmonth+"/"+nextyear+"'"); // populating
    }
}
document.getElementById('date1').value=''; //needed for firefox
</script>
</body>
</html>
[/code]
nb: you have to remove the 'u' in the line > var bits = re.execu(s);

Hoping it helps,,

l8tr,,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.