Use timestamps.
It's faster since you don't need to get the date, month and year. In the following script, the only function called for the date is getTime()
. Additionally, if you use the newer Date.now()
, you skip creating a date object.
In the example, we get the current timestamp and modulo it by a day. The remainder is the milliseconds that have passed since midnight. We then subtract it from the current timestamp so that the timestamp for the url is always based on the day. The only time it changes is when it's the next day.
Write to read
Sure, you're building a bookmarklet. But that does not mean making it unreadable. Write to read. Let the minifier do the shrinking for you. Besides, the bookmarklets themselves are copy-pasted, and not loaded. There's no overhead in size.
So in the following script, we pluck out the url so it's easily editable. We also make some parts verbose for easy reading.
The script
var url = 'http://bookmarklet.example.com/js/bookmarklet.js?v='
var currentTimestamp = new Date().getTime(); // or Date.now()
var currentDayTimestamp = currentTimestamp - (currentTimestamp % 86400000);
var script = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(script);
script.src = url + currentDayTimestamp;
Perf tests
Well, if you are bent on performance, then here's some perf tests.