Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: tween dates

  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts

    tween dates

    This is a simple function that you pass a couple of RFC strings containg dates or date and time information and the function returns the number of milliseconds between those dates

    Code:
    function tween(rfcDateBegin,rfcDateEnd){
    	rfcDateBegin = rfcDateBegin || false;
    	rfcDateEnd = rfcDateEnd || false;
    	if( rfcDateBegin && rfcDateEnd ) return Math.floor( new Date(rfcDateEnd).getTime() - new Date(rfcDateBegin).getTime() );
    return null;
    }
    Example:
    Code:
    // return the number of milliseconds in the year
    milliseconds = tween("1 Jan 2012","31 Dec 2012");
    Returns the number of milliseconds between the dates supplied, returns a negative value if the future date is passed first, allows you to determine from an input if a date is in the past or not.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by Cremator View Post
    Returns the number of milliseconds between the dates supplied, returns a negative value if the future date is passed first, allows you to determine from an input if a date is in the past or not.
    To be strictly accurate, returns a negative number if the DateEnd is before the DateBegin, but of course one or both both dates could be in the past.

    Could extend to show the number of days:-

    Code:
    milliseconds = tween("1 January 2012","31 Dec 2012");
    alert (milliseconds);
    days = milliseconds/86400/1000;
    days = days+1; // inclusive number of days in period
    alert (days);

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Yep, that is true and I have a modified version but stripped the function down to an elementary function that returns milliseconds which can be easily manipulated.

    Eg.
    Code:
    function tween_days(rfcDateBegin,rfcDateEnd){
    	rfcDateBegin = rfcDateBegin || false;
    	rfcDateEnd = rfcDateEnd || false;
    	if( rfcDateBegin && rfcDateEnd ) return tween(rfcDateBegin,rfcDateEnd)/86400000;
    	return null;
    }
    Its a nice little function that at first glance appears not to do much but offers plenty of scope in other functions or simply extending its use.
    Last edited by Cremator; 01-26-2012 at 04:41 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
    •