admin 管理员组文章数量: 1086019
I'm trying to get the current date so I can use it at the ending of this link. My code is almost correct but can't figure out what is going wrong.
- Correct Format Example: **;jspURL=clusterTools/toolset.jsp&fegoaltype=RAW&eedbListName=ZUM_CMP_DNS_CMP_Clean&facility=DMOS5-CLUSTER&monthDate=01/09/2022
My results are almost correct but the code returns with the following date 01/0/2022 instead of 01/09/2022
Can someone help me figure out this small bug?
<script>
const getDate = () => {
let newDate = new Date();
let year = newDate.getFullYear();
let month = newDate.getMonth() + 1;
let d = newDate.getDay();
return month + '/' + d + '/' + year;
}
document.getElementById('Ao').src =
';jspURL=clusterTools/toolset.jsp&fegoaltype=RAW&eedbListName=ZUM_CMP_DNS_CMP_Clean&facility=DMOS5-CLUSTER&monthDate='
.concat(getDate());
document.getElementById('MTD').src =
';jspURL=clusterTools/toolsetFe.jsp&fegoaltype=RAW&eedbListName=ZUM_DIFF_TEL_IPD_HTO&facility=DMOS5-CLUSTER&monthDate='
.concat(getDate());
</script>
I'm trying to get the current date so I can use it at the ending of this link. My code is almost correct but can't figure out what is going wrong.
- Correct Format Example: **http://zumdb-prod.itg./zum/AppServlet?action=aotool&jspURL=clusterTools/toolset.jsp&fegoaltype=RAW&eedbListName=ZUM_CMP_DNS_CMP_Clean&facility=DMOS5-CLUSTER&monthDate=01/09/2022
My results are almost correct but the code returns with the following date 01/0/2022 instead of 01/09/2022
Can someone help me figure out this small bug?
<script>
const getDate = () => {
let newDate = new Date();
let year = newDate.getFullYear();
let month = newDate.getMonth() + 1;
let d = newDate.getDay();
return month + '/' + d + '/' + year;
}
document.getElementById('Ao').src =
'http://zumdb-prod.itg./zum/AppServlet?action=aotool&jspURL=clusterTools/toolset.jsp&fegoaltype=RAW&eedbListName=ZUM_CMP_DNS_CMP_Clean&facility=DMOS5-CLUSTER&monthDate='
.concat(getDate());
document.getElementById('MTD').src =
'http://zumdb-prod.itg./zum/AppServlet?action=aotoolFe&jspURL=clusterTools/toolsetFe.jsp&fegoaltype=RAW&eedbListName=ZUM_DIFF_TEL_IPD_HTO&facility=DMOS5-CLUSTER&monthDate='
.concat(getDate());
</script>
Share
Improve this question
asked Jan 10, 2022 at 0:28
Med2020Med2020
391 silver badge6 bronze badges
2
- 1 "The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year)." developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – evolutionxbox Commented Jan 10, 2022 at 0:32
- 2 Consider using Intl DateTimeFormat instead? – evolutionxbox Commented Jan 10, 2022 at 0:33
2 Answers
Reset to default 4You should be using getDate()
and not getDay()
. The latter returns the zero-based day of the week (starting Sunday). You want to get the date of the month.
In order to ensure you get double digits for the month and day, you need to convert those numbers to string first, and then use String.prototype.padStart
.
const getDate = () => {
const newDate = new Date();
const year = newDate.getFullYear();
const month = newDate.getMonth() + 1;
const d = newDate.getDate();
return `${month.toString().padStart(2, '0')}/${d.toString().padStart(2, '0')}/${year}`;
}
console.log(getDate());
Here are alternative methods for native date formatting using toLocaleString()
m/d/yyyy
const getDate = () => {
const date = new Date();
return date.toLocaleString().split(",")[0];
}
console.log(getDate());
mm/dd/yyyy
const getDate = () => {
const date = new Date();
return date.toLocaleString('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
}
console.log(getDate());
本文标签: javascriptGet Current Date using JSStack Overflow
版权声明:本文标题:javascript - Get Current Date using JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744073445a2528913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论