Calculating difference between two dates using JS

Discussion in 'Javascript' started by Yotam, Nov 10, 2006.

  1. Yotam

    Yotam Guest

    Hi,
    I need some help with JS. I will be grateful, if you can help me out.
    I have two date fields (check in, check out) and "number of days"
    field.
    I want the script to calculate automatically the difference.
    For example: I have defaults dates, and I want the script to put the
    difference in "number of days" field. And if the user will change the
    date, the number of days will change automatically.


    Thanks,
    Yotam
    Yotam, Nov 10, 2006
    #1
    1. Advertising

  2. Yotam

    RobG Guest

    Yotam wrote:
    > Hi,
    > I need some help with JS. I will be grateful, if you can help me out.
    > I have two date fields (check in, check out) and "number of days"
    > field.
    > I want the script to calculate automatically the difference.
    > For example: I have defaults dates, and I want the script to put the
    > difference in "number of days" field. And if the user will change the
    > date, the number of days will change automatically.


    Everything you ever wanted to know about date arithmetic (and then
    some):

    <URL: http://www.merlyn.demon.co.uk/js-dates.htm >

    --
    Rob
    RobG, Nov 10, 2006
    #2
    1. Advertising

  3. Yotam

    Yotam Guest

    Hi,
    Thanks for your reaply.
    All the examples is this site
    (<URL:http://www.merlyn.demon.co.uk/js-dates.htm>) are with buttons
    (the user have to press a button for the function to calculate).

    I'm looking for a function that does that automatically.


    Thanks again,
    Yotam

    On Nov 10, 3:39 pm, "RobG" <> wrote:
    > Yotam wrote:
    > > Hi,
    > > I need some help with JS. I will be grateful, if you can help me out.
    > > I have two date fields (check in, check out) and "number of days"
    > > field.
    > > I want the script to calculate automatically the difference.
    > > For example: I have defaults dates, and I want the script to put the
    > > difference in "number of days" field. And if the user will change the
    > > date, the number of days will change automatically.Everything you ever wanted to know about date arithmetic (and then

    > some):
    >
    > <URL:http://www.merlyn.demon.co.uk/js-dates.htm>
    >
    > --
    > Rob
    Yotam, Nov 10, 2006
    #3
  4. Yotam

    Guest

    The date arithmetic problem is solved, I take it. All you need to do is
    attach the function to eg onchange events for your text fields.

    eg. something like
    <input name="checkin" onchange="doArithmetic()" />
    <input name="checkout" onchange="doArithmetic()" />

    Unfortunately, iirc onchange waits until input focus is moved away from
    the text field before triggering, so in my applications I've ended up
    using the onkeypress event. (I experimented with a few keyboard events,
    this one works best across the browsers.) This calls the function
    before updating the text element's value, so I also enclose the
    function in a setTimeout to make sure it sees the new value.
    elem.onkeypress = function() { setTimeout( 'somefunction(someargs)', 50
    ); };
    This waits 50ms before calling the function, by which time elem.value
    is at the newly entered value.

    HTH

    Yotam wrote:
    > Hi,
    > Thanks for your reaply.
    > All the examples is this site
    > (<URL:http://www.merlyn.demon.co.uk/js-dates.htm>) are with buttons
    > (the user have to press a button for the function to calculate).
    >
    > I'm looking for a function that does that automatically.
    >
    >
    > Thanks again,
    > Yotam
    >
    > On Nov 10, 3:39 pm, "RobG" <> wrote:
    > > Yotam wrote:
    > > > Hi,
    > > > I need some help with JS. I will be grateful, if you can help me out.
    > > > I have two date fields (check in, check out) and "number of days"
    > > > field.
    > > > I want the script to calculate automatically the difference.
    > > > For example: I have defaults dates, and I want the script to put the
    > > > difference in "number of days" field. And if the user will change the
    > > > date, the number of days will change automatically.Everything you ever wanted to know about date arithmetic (and then

    > > some):
    > >
    > > <URL:http://www.merlyn.demon.co.uk/js-dates.htm>
    > >
    > > --
    > > Rob
    , Nov 10, 2006
    #4
  5. In message <>, Fri,
    10 Nov 2006 06:01:37, Yotam <> writes
    >Hi,
    >Thanks for your reaply.
    >All the examples is this site
    >(<URL:http://www.merlyn.demon.co.uk/js-dates.htm>) are with buttons
    >(the user have to press a button for the function to calculate).
    >
    >I'm looking for a function that does that automatically.



    Here is a minimal demo of one way to do it, though setInterval is
    sub-elegant for the purpose.

    <input name=X> - <input name=Y> = <input name=Z readonly>
    <script>
    setInterval(
    "Z.value=Math.round((new Date(X.value)-new Date(Y.value))/864e5)",
    1000)
    </script>

    To be reasonably sure of further interest, read the FAQ and format your
    responses correctly.

    It's a good idea to read the newsgroup FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Nov 10, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    839
    Rob Schieber
    Sep 30, 2005
  2. celsius
    Replies:
    2
    Views:
    469
    Stan Milam
    Jan 5, 2005
  3. clintonb
    Replies:
    9
    Views:
    851
    James Kanze
    May 23, 2007
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    172
    Mark Schupp
    Aug 9, 2004
  5. Jochen Kaechelin

    calculating the difference of to dates

    Jochen Kaechelin, Aug 9, 2007, in forum: Ruby
    Replies:
    1
    Views:
    81
    Konrad Meyer
    Aug 9, 2007
Loading...

Share This Page