View Full Version : Add commas to Calculate difference btw the two dates script
justdave
11-28-2014, 07:12 PM
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:
<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
jscheuer1
11-28-2014, 09:22 PM
<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>
justdave
11-28-2014, 10:54 PM
Thank you, John. Exactly what I needed. Tried for several hours on my own, but couldn't make it work
JustDave
jscheuer1
11-29-2014, 06:35 PM
In case anyone wonders, there's a simpler method:
<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.
justdave
11-29-2014, 07:35 PM
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.
In case anyone wonders, there's a simpler method:
<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.
jscheuer1
11-29-2014, 08:00 PM
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:
<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.
justdave
11-29-2014, 09:04 PM
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.
jscheuer1
11-30-2014, 01:15 AM
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 . . .
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.