38

現在の日付から 10 日先の日付を自動的に設定し、dd/mm/yyyy の形式で表示する JavaScript を作成して、最終的に Selenium で使用されるようになることを願っています。

私は現在、以下のスクリプトを持っていますが、どこにも行きません:

var myDate=new Date();
myDate.now.format(myDate.setDate(myDate.getDate()+5),("dd/mm/yyyy");

どんな助けでも大歓迎です。

4

6 に答える 6

41

これは、将来の日付を取得する例です...

var targetDate = new Date();
targetDate.setDate(targetDate.getDate() + 10);

// So you can see the date we have created
alert(targetDate);

var dd = targetDate.getDate();
var mm = targetDate.getMonth() + 1; // 0 is January, so we must add 1
var yyyy = targetDate.getFullYear();

var dateString = dd + "/" + mm + "/" + yyyy;

// So you can see the output
alert(dateString);

日付をフォーマットするためのより適切な方法がいくつかあります。例は次の場所にあります。

http://www.west-wind.com/Weblog/posts/282495.aspx

http://www.svendtofte.com/javascript/javascript-date-string-formatting/

于 2010-08-26T06:48:24.643 に答える
3

私がすることは、DateHelper次のようなカスタムオブジェクトを作成することです:

var DateHelper = {
    addDays : function(aDate, numberOfDays) {
        aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
        return aDate;                                  // Return the date
    },
    format : function format(date) {
        return [
           ("0" + date.getDate()).slice(-2),           // Get day and pad it with zeroes
           ("0" + (date.getMonth()+1)).slice(-2),      // Get month and pad it with zeroes
           date.getFullYear()                          // Get full year
        ].join('/');                                   // Glue the pieces together
    }
}

// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Add 10 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), 10));

この Fiddleも参照)

于 2016-03-09T14:50:25.103 に答える