Heim  >  Fragen und Antworten  >  Hauptteil

Wie erhalte ich das aktuelle Datum in JavaScript?

<p>Wie erhalte ich das aktuelle Datum in JavaScript? </p>
P粉463291248P粉463291248423 Tage vor466

Antworte allen(2)Ich werde antworten

  • P粉742550377

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

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

    如果您要重复使用 utc 变量(例如 new Date(utc)),请使用 replace 选项,如 Firefox 和Safari 无法识别带破折号的日期。

    Antwort
    0
  • P粉618358260

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

    使用new Date()生成一个包含当前日期和时间的新Date对象。

    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);

    Antwort
    0
  • StornierenAntwort