Results 1 to 11 of 11

Thread: Javascript: how do I add seconds to a date? [resolved]

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Question Javascript: how do I add seconds to a date? [resolved]

    how do I add seconds to a date?

    I'm just starting to work with Javascript but I need this pretty fast.

    I need to design a Javascript function that works something like VB's dateadd() function.
    It only needs to do seconds so it would look something like this:

    function addSeconds(myDate, Seconds)
    {
    //Needed Code...newDate = myDate + Seconds;

    return newDate;
    }

    Alternately, a function that just adds xx seconds to the current date will work.

    function DatePlusSecs(Seconds)
    {
    var myDate = new Date();
    //Needed Code....newDate = myDate + Seconds;
    return newDate;
    }

    I've been searching web sites for this but haven't found anything that i could understand/make work.
    Last edited by longwolf; May 27th, 2003 at 05:47 PM.

  2. #2
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    I'm not really familiar with VB's addDate() function.

    Could you specify the date format you are using? Is it a string? Is your date a number in minutes?
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  3. #3

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    I'm using:
    myDate = new Date();
    as part of a timer loop.

    I need to add XX seconds to myDate so I can use:
    if (Date() > myDate) {}
    to trigger an action.

    I'd like to be able to display myDate to the user in a format that makes sense to them. i.e. date and time
    Last edited by longwolf; May 25th, 2003 at 11:59 PM.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To trigger a delayed action use setTimeout().
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    I guess I need to make things a bit more clear.
    I writing a fairly complex program with javascript.
    There is a main loop that can call several subs depending on what happens.

    Some of those sub-functions must not run too often.
    That's why I need be able to add xx seconds to a variable that holds a date for testing against Date().

    I also need to be able to pull up that date variable and look at it.

    This is actually a script that's followed by a C++ program.
    I did try using setTimeout() to controll a flag, but the C++ app gave me "setTimeout is not defined"

    But that command would not have given me all that I need anyway

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This is actually a script that's followed by a C++ program.
    What do you mean by that? Who interprets the JavaScript?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    Originally posted by CornedBee
    What do you mean by that? Who interprets the JavaScript?
    I worded that badly.

    It's part of a macro-ing program.

    The C++ app is a "go-between" program.
    It tracks and interprets what's happening in a 3rd program and can send commands to the 3rd program.

    It has it's own function calls that the Javascript uses to get values from the 3rd program and more functions to tell the C++ app which commands to send to the 3rd program.

    It also has some error checking that it uses to tell you when it finds an error in your Javascript.

    I don't have the source code for the C++ app (wish I did) so I can't tell you any more about it.

    Here's the silly part. Like I said, I'm new to Javascript and at this point I know more of the C++ app's function calls than I know of Javascript functions : D

    That's why I'm asking someone to show me how to "add xx seconds to a date variable"

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    devedge.netscape.com has a complete JavaScript reference somewhere.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    Originally posted by longwilf
    I've been searching web sites for this but haven't found anything that i could understand/make work.
    I've been studying a Lot of tutorial sites.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    devedge is a reference, not a tutorial.

    Quite easy to figure out after reading a bit and trying a bit:
    Code:
    function addSecs(d, s) {
      return new Date(d.valueOf()+s*1000);
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343
    Originally posted by CornedBee
    devedge is a reference, not a tutorial.

    Quite easy to figure out after reading a bit and trying a bit:
    Code:
    function addSecs(d, s) {
      return new Date(d.valueOf()+s*1000);
    }
    Easy for you maybe : )

    From the sites I looked at it seemed like I'd have to build the a date step at a time.

    This is GREAT!

    Thx CornedBee

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