-1

jQueryカレンダーから日付を取得し、それを年-月-日にフォーマットする関数があります。

カレンダーで取得する日付は、dateStringの場合は2013年3月4日で、2013年3月4日の形式で表示されます。しかし、start_dateで取得する日付は2013-21-04です。大丈夫だったので不思議だと思います。

function makeUpDates() {
    // concantenate values to date_start and date_end hidden inputs
    var dateString = document.getElementById('date').value, date = new Date(dateString);
    document.getElementById('date_start').value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + ("0" + date.getDate()).slice(-2);
    var numDays = document.getElementById('slider').value;
    date.setDate(date.getDate() + parseInt(numDays));
    var dateEnd = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + ("0" + date.getDate()).slice(-2);
    document.getElementById('date_end').value = dateEnd;
}
4

2 に答える 2

1

このように日付を変換できます

//will take a date in the form MM/DD/YYYY and return YYYY-MM-DD
function convertDate(dateString){
    var dateparts = dateString.split("/");
    var newDate = dateparts[2]+"-"+dateparts[0]+"-"+dateparts[1]
    return newDate;
}

また、より一般的な日付処理については、このトピックに関するより一般的な質問へのこの回答で言及されているmoment.jsをチェックしてください。

于 2013-03-04T18:31:51.377 に答える
1

start_dateは2013-21-04です

おそらく、文字列連結の括弧を忘れて、代わりにdate.getMonth()+1降伏します。ただし、投稿したスニペットで修正されているようです。213

于 2013-03-04T18:35:01.207 に答える