2

日付を解析したい(文字列の日付形式からJavaScriptの日付形式へ)が、などのようなさまざまな文字列の日付形式がYYYY-MM-DDありDD-MM-YYYYます'DD/MM/YYYY'..

これらの文字列の日付形式から JavaScript の日付形式に変換する一般的な手順または方法はありますか?

4

4 に答える 4

2

これらは、私がそのために使用する2つのライブラリです。

于 2012-11-05T10:51:47.750 に答える
1

ここでuserFormat、文字列はこれらの形式'DD-MM-YYYY'、などになります'YYYY-MM-DD''DD/MM/YYYY'

function parseDate(dateString, userFormat) {
    var delimiter, theFormat, theDate, month, date, year;
    // Set default format if userFormat is not provided
    userFormat = userFormat || 'yyyy-mm-dd';

    // Find custom delimiter by excluding
    // month, day and year characters
    delimiter = /[^dmy]/.exec(userFormat)[0];

    // Create an array with month, day and year
    // so we know the format order by index
    theFormat = userFormat.split(delimiter);

    //Create an array of dateString.
    theDate = dateString.split(delimiter);
    for (var i = 0, len = theDate.length; i < len; i++){
      //assigning values for date, month and year based on theFormat array.
      if (/d/.test(theFormat[i])){
        date = theDate[i];
      }
      else if (/m/.test(theFormat[i])){
        month = parseInt(theDate[i], 10) - 1;
      }
      else if (/y/.test(theFormat[i])){
        year = theDate[i];
      }
    }
    return (new Date(year, month, date));
}
于 2012-11-05T10:40:37.277 に答える
0

ただdatejsを入手してください。

http://www.datejs.com/

...そして、独自の Javascript 日付関数を再び記述する必要はありません。

于 2012-11-05T10:42:47.097 に答える
-1
function dateParsing(toDate, dateFormat) {
    var dt = Date.parseInvariant(todate, dateFormat); // pass date and your desired format e.g yyyy/M/dd            
    alert(dt); // to check date
}
于 2012-11-05T11:46:53.720 に答える