日期书签需要在单个数字的月份和日期前导零

时间:2021-03-06 22:08:48

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

例如,我的bookmarklet提供:http://url/log/2009-5-4_localcontrol-story.log,而日志文件实际上位于: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?

你能告诉我对此的改编吗?如果有必要,我可以用2位数字格式获得前导零点的月份和日期吗?

1 个解决方案

#1


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)

(添加前导零,切片(-2)取最后2个字符)

#1


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)

(添加前导零,切片(-2)取最后2个字符)