我正在嘗試獲取當前日期,以便可以在此鏈接的末尾使用它。我的代碼幾乎是正確的,但無法弄清楚出了什么問題。
- 正確格式示例: ** http://zumdb-prod.itg.com/zum/AppServlet?action=aotool&jspURL=clusterTools/toolset.jsp&fegoaltype=RAW&eedbListName=ZUM_CMP_DNS_CMP_Clean&facility=DMOS5-CLUSTER&monthDate=01/09/2022
我的結果幾乎是正確的,但代碼回傳以下日期 01/0/2022 而不是 01/09/2022
有人可以幫我找出這個小錯誤嗎?
<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.com/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.com/zum/AppServlet?action=aotoolFe&jspURL=clusterTools/toolsetFe.jsp&fegoaltype=RAW&eedbListName=ZUM_DIFF_TEL_IPD_HTO&facility=DMOS5-CLUSTER&monthDate='
.concat(getDate());
</script>
uj5u.com熱心網友回復:
你應該使用getDate()而不是getDay(). 后者回傳從零開始的一周中的一天(從星期日開始)。您想獲取月份的日期。
為了確保您獲得兩位數的月份和日期,您需要先將這些數字轉換為字串,然后使用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());
uj5u.com熱心網友回復:
以下是使用原生日期格式化的替代方法 toLocaleString()
月/日/年
const getDate = () => {
const date = new Date();
return date.toLocaleString().split(",")[0];
}
console.log(getDate());
月/日/年
const getDate = () => {
const date = new Date();
return date.toLocaleString('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
}
console.log(getDate());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407192.html
標籤:
