search

Home  >  Q&A  >  body text

How to get current date in JavaScript?

<p>How to get the current date in JavaScript? </p>
P粉463291248P粉463291248460 days ago497

reply all(2)I'll reply

  • P粉742550377

    P粉7425503772023-08-24 09:11:26

    var utc = new Date().toJSON().slice(0,10).replace(/-/g,'/');
    document.write(utc);

    If you want to reuse a utc variable (e.g. new Date(utc)), use the replace option, as Firefox and Safari do not recognize dashes date.

    reply
    0
  • P粉618358260

    P粉6183582602023-08-24 00:07:53

    Use new Date()Generate a new Date object containing the current date and time.

    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();
    
    today = mm + '/' + dd + '/' + yyyy;
    document.write(today);

    reply
    0
  • Cancelreply