Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date bookmarklet needs leading zero on single digit months and days

I have a bookmarklet I use to check daily log files. However the bookmarklet I use only delivers the month and day in single digits, however the log files use double digits.

For example my bookmarklet delivers: http://url/log/2009-5-4_localcontrol-story.log, while the log file actually lives at: http://url/log/2009-05-04_localcontrol-story.log

Below is my current code:

javascript:d=new%20Date();window.open("http://url/log/"+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+(d.getDate())+"_localcontrol-story.log",%20"_self");

Can you tell me an adaptation to this so I get my month and date in 2 digit format with the leading zero if necessary?


1 Answers

it's kind of a pain, but what I've done is to do stuff like this:

("0"+d.getDate()).slice(-2)

(add a leading zero, and slice(-2) takes the last 2 characters)

like image 185
Jason S Avatar answered Dec 10 '25 02:12

Jason S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!