Results 1 to 8 of 8

Thread: Add commas to Calculate difference btw the two dates script

  1. #1
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Add commas to Calculate difference btw the two dates script

    Hello everyone

    I have a script on my personal home page that counts the days between 10/19/1962 and Today. It works fine but I would like the output to have thousands commas. Can someone help? I would need to be able to copy and paste your help as I am not a programmer. Here is the script:

    Code:
    <script type="text/javascript">
    
    //Set the two dates
    var millennium =new Date(1962, 9, 19) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24
    
    //Calculate difference btw the two dates, and convert to days
    document.write(Math.ceil((today.getTime()-millennium.getTime())/(one_day))+
    " days")
    
    </script>
    Thanks for any help
    JustDave

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    
    //Set the two dates
    var millennium =new Date(1962, 9, 19) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24
    
    //Calculate difference btw the two dates, and convert to days
    var format_days_since = (Math.ceil((today.getTime()-millennium.getTime())/(one_day)) / 1000).toString(10).replace(/\./, ',');
    if(format_days_since.length < 3){
    	format_days_since += ',';
    }
    while (format_days_since.length < 6){
    	format_days_since += 0;
    }
    document.write(format_days_since + " days");
    
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you, John. Exactly what I needed. Tried for several hours on my own, but couldn't make it work
    JustDave

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In case anyone wonders, there's a simpler method:

    Code:
    <script type="text/javascript">
    
    //Set the two dates
    var millennium =new Date(1962, 9, 19) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24
    
    //Calculate difference btw the two dates, and convert to days
    var format_days_since = (Math.ceil((today.getTime()-millennium.getTime())/(one_day)) / 1000).toFixed(3).replace(/\./, ',');
    
    document.write(format_days_since + " days");
    
    </script>
    Both of these methods assume that, since even after 100 more years, the number being parsed will still be less than 56,000, that nothing requiring more than one comma need be dealt with. If it were (like 40,345,023 for example) - a slightly more involved method would likely be required. At the very least, a different method.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    John, thanks for that.
    Either method works fine for my situation, but with either method if the result would be less than 1000, there is a leading 0, ie; 0,999.
    Just thought I'd mention that in case anyone else wants to use the code for a more recent date.

    Quote Originally Posted by jscheuer1 View Post
    In case anyone wonders, there's a simpler method:

    Code:
    <script type="text/javascript">
    
    //Set the two dates
    var millennium =new Date(1962, 9, 19) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24
    
    //Calculate difference btw the two dates, and convert to days
    var format_days_since = (Math.ceil((today.getTime()-millennium.getTime())/(one_day)) / 1000).toFixed(3).replace(/\./, ',');
    
    document.write(format_days_since + " days");
    
    </script>
    Both of these methods assume that, since even after 100 more years, the number being parsed will still be less than 56,000, that nothing requiring more than one comma need be dealt with. If it were (like 40,345,023 for example) - a slightly more involved method would likely be required. At the very least, a different method.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ah, well I just assumed time would not go backward. Anyways, I just finished working out a method that works for numbers of virtually any size (ex: 90006001002003007008.8765) which also works with numbers less than 1000:

    Code:
    <script type="text/javascript">
    
    //Set the two dates
    var millennium =new Date(1962, 9, 19) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24
    function parseout3digitgroups(n, d){
    	d = d || ',';
    	n = n.toString(10).split('.');
    	n[0] = n[0].split('');
    	var nn = n[0].length, c = 0, r = '';
    	while(--nn > -1){
    		r = n[0][nn] + r;
    		if((++c) % 3 === 0 && nn){
    			r = d + r;
    		}
    	}
    	return r + (n[1]? '.' + n[1] : '');
    }
    //Calculate difference btw the two dates, and convert to days
    var format_days_since = parseout3digitgroups(Math.ceil((today.getTime()-millennium.getTime())/(one_day)));
    
    document.write(format_days_since + " days");
    
    </script>
    I will probably make this simpler later. But it might be about it - that is if you need it to be able to do real large and/or less than 1000 and/or anything in between.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't need it for large numbers, but others might. The first solution you provided was adequate for me and thanks again for that
    JustDave.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Oh yeah, sure - and you're welcome. You don't know me well yet - but I often get into posting several and hopefully continually more widely applicable and/or simple solutions for things. I wouldn't have posted the first one if I didn't think it was good enough for what you asked. Thankfully it was. I'm not always right on the first shot - but usually, these days . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 1
    Last Post: 04-16-2011, 02:15 PM
  2. Universal Countdown Script - Multiple Dates
    By nosix in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 11-14-2010, 09:08 AM
  3. How to calculate time between two dates
    By NDK in forum JavaScript
    Replies: 1
    Last Post: 06-19-2009, 12:50 PM
  4. calculate due dates
    By vikaspa in forum JavaScript
    Replies: 1
    Last Post: 01-11-2009, 11:22 AM
  5. Looking for calendar script with different backgr colors for available/reserved dates
    By monique in forum Looking for such a script or service
    Replies: 1
    Last Post: 10-03-2007, 09:29 AM

Bookmarks

Posting Permissions

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