Jump to content

Recommended Posts

Below is my code. It calculates the dates & post the in my PHP form. If I type in a date in the sent_date field it calculates the correct dates according to the math in the code. My question is, is there any way to edit this code so if one of the calculated dates falls on a Saturday or Sunday, the date displayed will be the previous Fridays date?

 

<script type="text/javascript">
var valid;

function d2(v) { return (v<10)?("0"+v):v; }

function dcheck(form) {
var s = form.sent_date.value;
var dr = form.due_rev.value;
var su = form.due_suspo;
var clk = form.due_clerk;
var att = form.due_attny;
var jdg = form.due_owner;
var sent = new Date(s);
var due_rev = new Date(dr);
var due_suspo = new Date(su);
var due_clerk = new Date(clk);
var due_attny = new Date(att);
var due_owner = new Date(jdg);

if (isNaN(due_rev)) {
due_rev = new Date(due_suspo.getFullYear(),due_suspo.getMonth(),due_suspo.getDate()-2);
}
if (isNaN(due_suspo)) {
due_suspo = new Date(due_clerk.getFullYear(),due_clerk.getMonth(),due_clerk.getDate()-2);
}
if (isNaN(due_clerk)) {
due_clerk = new Date(due_attny.getFullYear(),due_attny.getMonth(),due_attny.getDate()-2);
}
if (isNaN(due_attny)) {
due_attny = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-36);
}
if (isNaN(due_owner)) {
due_owner = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-7);
}

form.sent_date.value = (sent.getFullYear()) + "-" + d2(sent.getMonth()+1) + "-" + d2(sent.getDate());
form.due_rev.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-6);
form.due_suspo.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-4);
form.due_clerk.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-2);
form.due_attny.value = d2(due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate());
form.due_owner.value = (due_owner.getFullYear()) + "-" + d2(due_owner.getMonth()+1) + "-" + d2(due_owner.getDate());

return true;
}

</script>

Link to comment
https://forums.phpfreaks.com/topic/232669-need-jscript-help-please/
Share on other sites

When I first looked at this I thought it was going to be simple, but quickly realised it wasn't with what's available in the JavaScript Date object -- Google weren't much use either. So I decided to write a prototype function that could do it (name needs a bit more thought like):

 

Date.prototype.setAsPrevFridayIfWeekend = function() {
    // Convert date to previous Friday's if a weekend
    if (this.getDay() == 0 || this.getDay() == 6) {
        var move_back = (this.getDay() == 0) ? 2 : 1;
        if (this.getDate() <= move_back) {
            if (this.getMonth() == 0) {
                this.setMonth(11);
                this.setFullYear(this.getFullYear() - 1);
            } else {
                this.setMonth(date.getMonth - 1);
            }
            this.setDate(this.getDaysInMonth() - (move_back - this.getDate()));
        } else {
            this.setDate((this.getDate() - move_back));
        }
    }
}

 

Within this though I had to add another prototype function to work out the number of days in a month, so I borrowed some code from about.com and rewrote it as a prototype function:

 

Date.prototype.getDaysInMonth = function() {
    var m = [31,28,31,30,31,30,31,31,30,31,30,31];
    if (this.getMonth() != 1) return m[this.getMonth()];
    if (this.getYear()%4 != 0) return m[1];
    if (this.getYear()%100 == 0 && this.getYear()%400 != 0) return m[1];
    return m[1] + 1;
}

 

So now you can simply include those two prototype functions and call the setAsPrevFridayIfWeekend Date method (as I said the name needs some work), to convert it to the Friday before if it's a weekend.

 

Some test cases (remember months are from 0-11, not 1-12 in JavaScript):

 

window.onload = function() {
    var date1 = new Date(2011, 0, 1); // Saturday 1st January 2011
    date1.setAsPrevFridayIfWeekend();
    alert(date1.toString()); // Fri 31st December 2010

    var date2 = new Date(2011, 3, 10); // Sunday 10th April 2011
    date2.setAsPrevFridayIfWeekend();
    alert(date2.toString()); // Fri 8th April 2011

    var date3 = new Date(2011, 3, 5); // Tuesday 5th April 2011
    date3.setAsPrevFridayIfWeekend();
    alert(date3.toString()); // Tuesday 5th April 2011
}

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.