Results 1 to 8 of 8

Thread: Javascript's equilvalent of DateDiff Function?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Javascript's equilvalent of DateDiff Function?

    I have a scenario on a web page that compares 2 date fields. When dates are entered their cannot be a difference of more than 365 days. I know VB.Net has the DateDiff() function. I'm pretty new to using Javascript. I've done some searching and couldn't find a DateDiff() function for JS. Was wondering if their is anything like the DateDiff() or does anyone have a homegrown function that will return the difference in dates written in Javascript?

    Thanks,
    Blake

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Javascript's equilvalent of DateDiff Function?

    What format are your dates in, first of all?
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Javascript's equilvalent of DateDiff Function?

    The dates are formatted like this

    mm/dd/yyyy or m/d/yyyy

    Bottom line is...there has to be a slash and not a . or -
    Blake

  4. #4
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: Javascript's equilvalent of DateDiff Function?

    Hello

    when we compare between strings we should start from the beginning of them and this requires the date format to be yyyy/mm/dd or (yyyy/m/d), but no problem, in this case and if I were you, I would use the split() function which return your string as an array according to your separator argument:

    example:

    HTML Code:
    <script type="text/javascript">
    var date1 = "01/23/1992";
    var date2 = "11/01/1991";
    var compare1 = date1.split("/");
    var compare2 = date2.split("/");
    // compare1[0] = "01";
    // compare1[1] = "23";
    // compare1[2] = "1992";
    // compare2[0] = "11";
    // compare2[1] = "01";
    // compare2[2] = "1991";
    </script>
    as you see we have separated each part of the date so we can compare it easily with another string

    Q) what if the day was for example was 1 not 01
    A) to solve this problem, when you compare, convert these strings to numbers like this:

    HTML Code:
    <script type="text/javascript">
    if (parseInt(compare1[1]) > parseInt(compare2[1])) {
        // the first day is bigger
    }
    </script>
    the parseInt(string, [base]) function parses its first argument, a string, and attempts to return an integer of the specified radix (base).

    the last line source is: Core JavaScript reference 1.5

    regards

    Feras Jobeir
    Last edited by fjober; Aug 26th, 2011 at 07:52 PM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Javascript's equilvalent of DateDiff Function?

    fjober,

    I actually ended up doing this. I kinda like the Split function as I use that alot in VB. The parseInt I used exactly the same way your illustrating. That solved the part of my problem that occurred first. Now for the other part and that's a complete Date Validation routine.

    Thanks for your input!
    Blake

  6. #6
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: Javascript's equilvalent of DateDiff Function?

    When dates are entered their cannot be a difference of more than 365 days
    ok sir it is easy, I will do it then replay

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Javascript's equilvalent of DateDiff Function?

    Sound good.

    Thanks again!
    Blake

  8. #8
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: Javascript's equilvalent of DateDiff Function?

    try this function please, return true if the difference is less than a year:

    HTML Code:
    <script type="text/javascript">
        // mm/dd/yyyy
        var validDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        function dateDifference(date1, date2)
        {
            if (validDate.test(date1) && validDate.test(date1)) {
                date1Numbers = date1.split("/");
                date2Numbers = date2.split("/");
                var month1 = date1Numbers[0];
                var month2 = date2Numbers[0];
                var day1 = date1Numbers[1];
                var day2 = date2Numbers[1];
                var yearDif = Math.abs(date1Numbers[2] - date2Numbers[2]);
                if (yearDif <= 1) {
                    if (yearDif == 1) {
                        if (month2 <= month1) {
                            if (month2 == month1) {
                                if (day2 <= day1) {
                                    calculateDiff(date1Numbers, date2Numbers);
                                    return true;
                                } else {
                                    return false;
                                }
                            } else {
                                calculateDiff(date1Numbers, date2Numbers);
                            }
                        } else {
                            return false;
                        }   
                    } else {
                        calculateDiff(date1Numbers, date2Numbers);
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }  
        }
        
        function calculateDiff(date1Numbers, date2Numbers)
        {
            var resultAsDays = 0;
            var day1 = parseInt(date1Numbers[1]);
            var month1 = parseInt(date1Numbers[0]);
            var year1 = parseInt(date1Numbers[2]);
            var day2 = parseInt(date2Numbers[1]);
            var month2 = parseInt(date2Numbers[0]);
            var year2 = parseInt(date2Numbers[2]);
            
            if (year1 == year2) {
                resultAsDays += (Math.abs(month2 - month1)) * 30;
                resultAsDays -= day1;
            } else {
                if (month1 == month2) {
                    resultAsDays += 330;
                } else {
                    resultAsDays += (12 - (month1 - 1) + month2) * 30;
                    resultAsDays -= day1;
                }
            }
            resultAsDays += day2;
            window.alert(resultAsDays);
        }
        </script>
    
    <input type="text" id="date1" />
    <input type="text" id="date2" />
    
    <input type="button" value="is valid" onclick="window.alert(dateDifference(document.getElementById('date1').value, document.getElementById('date2').value));" />
    hope this to be useful

    please try it, I have tested it on some dates and I think it works good

    regards

    Feras Jobeir
    Last edited by fjober; Aug 27th, 2011 at 02:02 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width