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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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