2012-01-06 7 views
0

私はフォームの中で日付を取得するためにテキストフィールドを使用しています。これは、日付検証(mm/dd/yyyy)の下にjqueryコーディングを使用します。jqueryを使用して01/01/2010と2010/01/01の日付形式をサポートするには?

function isDate(txtDate) { 
    var objDate, // date object initialized from the txtDate string 
     mSeconds, // txtDate in milliseconds 
     day,  // day 
     month, // month 
     year;  // year 
    // date length should be 10 characters (no more no less) 
    if (txtDate.length !== 10) { 
     return false; 
    } 
    // third and sixth character should be '/' 
    if (txtDate.substring(2, 3) !== '/' || txtDate.substring(5, 6) !== '/') { 
     return false; 
    } 
    // extract month, day and year from the txtDate (expected format is mm/dd/yyyy) 
    // subtraction will cast variables to integer implicitly (needed 
    // for !== comparing) 
    month = txtDate.substring(0, 2) - 1; // because months in JS start from 0 
    day = txtDate.substring(3, 5) - 0; 
    year = txtDate.substring(6, 10) - 0; 
    // test year range 
    if (year < 1000 || year > 3000) { 
     return false; 
    } 
    // convert txtDate to milliseconds 
    mSeconds = (new Date(year, month, day)).getTime(); 
    // initialize Date() object from calculated milliseconds 
    objDate = new Date(); 
    objDate.setTime(mSeconds); 
    // compare input date and parts from Date() object 
    // if difference exists then date isn't valid 
    if (objDate.getFullYear() !== year || 
     objDate.getMonth() !== month || 
     objDate.getDate() !== day) { 
     return false; 
    } 
    // otherwise return true 
    return true; 
} 

これは、日付形式(YYYY/MM/DD)のために正常に動作します..しかし、今私は(M/D/YYYY)のような形式をサポートする必要があります。

どうすればいいですか?

これらの修飾を有する

答えて

0

あなたの例では、両方のフォーマットをサポートしています:

// date length should be between 8 and 10 characters (no more no less) 
if (txtDate.length < 8 || txtDate.length > 10) { 
    return false; 
} 

var tokens = txtDate.split('/'); 
// there should be exactly three tokens 
if (tokens.length !== 3) { 
    return false; 
} 

// extract month, day and year from the txtDate 
month = parseInt(tokens[0]) - 1; // because months in JS start from 0 
day = parseInt(tokens[1]); 
year = parseInt(tokens[2]); 
+0

yes.its working great..thanks –

+0

良い答えですが、後でm/d/yy、またはヨーロピアンスタイルのdd/mm/yyyyをサポートする必要がある場合はどうすればよいですか?あなたは絶えず新しいフォーマットをサポートする必要があり、毎回ホイールを再発明しています。ライブラリーIMHOを使う方がいいです。 – Richard

+0

@リチャード私は個人的にそれを行うだろう。しかし、場合によっては、新しい図書館を導入しない理由があるかもしれません。そして、もちろん、あなたは常に、簡単な/ハッキー/単純なソリューションと未来のソリューションのバランスを取っています。 – hleinone

0

では、次のようなコードを書くことができdate.jsライブラリそれに

をお試しください:

Date.parse('7/1/2004')   // Thu Jul 01 2004 

すべての解析文化情報を含むファイルを含めることで、Date.jsのグローバル化が可能になるので、bあなたが望むだけのことをすることができます。

あなたがしなければならないのは、あなたのページに以下のファイルを始めるために含まれている:

<script type="text/javascript" src="date.js"></script> 
<script type="text/javascript" src="date-de-DE.js"></script> 

EDIT

Date.jsは今死んでいるようだが、Moment.jsは次のようになります良い選択肢。

+0

date.jsはかなり死んだプロジェクトです。作者が日付処理ライブラリーを使用したい場合は、代わりに[moment.js](http://momentjs.com/)をお勧めします。 – hleinone

+0

@hleinone Brill!私は瞬間については聞いたことがありません.js、しかし素晴らしいヒント。 Javascriptブックマークのリストに追加しました。 – Richard

関連する問題