www.webdeveloper.com
Results 1 to 14 of 14

Thread: Date Function

  1. #1
    Join Date
    Jul 2012
    Posts
    8

    Date Function

    I am working with a proprietary blend of Javascript and have no idea what I am doing.

    What I am attempting to do is to take the value from a field (paydate) and convert it to a numerical form.

    Next, I need to take a different value - from another field (ach adjust) - and add it to the original value to produce a new value (paydate + ach adjust)

    The resulting numerical value needs to be converted to the corresponding day of the week.

    Since the days can result in a large enough time frame, instead of the GetDay function, I used the GetDate function. Here is what I have:

    var pd = paydate.getDate();

    // coverts the value from the field "paydate" to a numerical value within
    // the pd variable.

    var adj = clientMASTER_assoc_adjachdays;

    // converts the value from the field "clientMASTER_assoc_adjachdays" to
    // a variable. This value is already in a numerical form.

    var comp = (pd+adj);

    // converts the value resulting from the addition of the values within the
    // two variables, pd and adj, to the variable comp.

    comp;

    // outputs the variable.

    This script would work perfectly if I just wanted a numerical value (from 1 to 31), but I need the actual day of the week (Sun..Mon...Tue...Wed...Thur...Fri...Sat). How can I accomplish this?


  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    dateobject.getDay() returns 0-6 for Sun-Sat respectively.

  3. #3
    Join Date
    Jul 2012
    Posts
    8
    Great. But, if the value of the additional input is 0 or anything greater than 1 it messes with the result; (5 + 0 = 5), (5 + 2 = 7), (5 + 3 = 8)...

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Before you use it do a % 7 on it. Using a mod it will always be in range.

  5. #5
    Join Date
    Jul 2012
    Posts
    8
    I don't understand. Could you please put that into understandable context?

  6. #6
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    dateSub = dateSub % 7

    If your dateSub is < 7 nothing happens to it. If dateSub is larger than that it will be reduced to the 0-6 range.

  7. #7
    Join Date
    Jul 2012
    Posts
    8
    Given the code mentioned previously where would you add the datasub function?

  8. #8
    Join Date
    Jul 2012
    Posts
    8
    Bear in mind (in case I did not already mention it) that this is a proprietary blend of Javascript. In the situation that we previously discussed, I finished with the following code:

    var pd = paydate.getDay();

    // converts the value from the "paydate" field to an integer.

    var adj = clientMASTER_assoc_adjachdays;

    // converts the integer from the "clientMASTER_assoc_adjachdays" field to a variable.

    var comp = (pd+adj);

    // adds the two variables, pd and adj together, and converts the result to a variable.

    comp = comp%6;

    // constrains the results of the previous mathematical equation to a value of // less than or equal to 6.

    var arDays = new Array("Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday");

    // opens an array of the days of the week (0=Sunday,1=Monday...)

    var achd = arDays[comp];

    //compares the results of the comp variable to the arDays array and converts the result to another variable; the day of the week.

    achd;

    //outputs the achd variable

    ... In case anyone else ever needs to know.


  9. #9
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Make that
    Code:
    comp = comp &#37; 7
    . The range will be from 0 thru 6. See messages 4 & 6 in this thread.

  10. #10
    Join Date
    Jul 2012
    Posts
    8
    Another "expert" advised me that I should instead use "comp = comp&#37;6". Sorry. I updated as per your recommendation.

  11. #11
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    He must live in a place where there are only six days in a week. <g> Glad to hear it works now.

  12. #12
    Join Date
    Jul 2012
    Posts
    8
    Well, the array does consist of only the numbers 1 - 6 (0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday). Could you please explain why you need a 7 for all of us poor, uneducated people?

  13. #13
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Take any day of the month and note the day of the week. Now add or subtract 7, 14, 21, or 28. Won't it be the same day of the week?

    Take any positive integer and divide it by seven. If you ignore the quotient and pay attention only to the remainder, how big can it get and how small can it get? If you chose 13, the remainder would be 6. Add 1 to it, 14 is evenly divisible by 7 so the remainder will be zero.

  14. #14
    Join Date
    Jul 2012
    Posts
    8
    I can't see any fault in your logic. Thanks, Wbport for explaining that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles