2016-07-18 8 views
0

node.jsで次の日の日付を見つけなければなりません。そのmondayそれは2016-07-18をする必要がありますnode.jsの次の名前の日付を見つける方法

は、私は次の特定の日の日付(例:monday, tuesday、など)と仮定

考慮を見つける必要があり、2016-07-13(wednesday)として日付を考えてみましょう。

EDIT:

は、最寄りの月曜日(希望)日が何であるかを、この日付に基づいて、日付2016年7月13日を考えてみましょう。私はどうなるのか

EDIT

var target; 
     if(desiredIndex < moment(start).day()){ 
      target = moment(start).day(7 + desiredIndex).toDate() 
     } else { 
      target = moment(start).day(desiredIndex).toDate(); 
     } 

答えて

0

Moment.jsを使用してmoment.add(1, 'days').calendar();操作を使用しています。

私のやり方は、関数内に結果を日付オブジェクトに保存して、オブジェクトに望む曜日が含まれるまでforループで1つずつ追加してから、その日付を返します。

+0

ありがとう.. moment.add(1、 'days')。calendar();日を返しますか? – Subburaj

+0

また、forループ??どのデータをループする必要がありますか? – Subburaj

1

moment.jsを使用し、.day()を使用して目標曜日を以下のように設定します。 7を加えて「来週」のセマンティクスを強制します。

const moment = require('moment') 
const dayOfWeekIndex = [ 
    'sunday', 
    'monday', 
    'tuesday', 
    'wednesday', 
    'thursday', 
    'friday', 
    'saturday'] 
const start = '2016-07-13' 
const desired = 'tuesday' // change to suit taste. 
const desiredIndex = dayOfWeekIndex.indexOf(desired) 
// 7 means "next week" 
const target = moment(start).day(7 + desiredIndex).toDate() 
console.log(target) 
+0

ありがとうございます。水曜日(2016-07-13)と希望日は2016-07-18(来週日付)を返すはずです。上記のコードでは、シナリオのために動作しますか? – Subburaj

+0

現在の日付に基づいて戻ります。日付に基づいて返すことができます。私の編集した投稿をご覧ください。 – Subburaj

+0

はいそれに応じてコードを更新しました。日付や文字列を 'moment()'に渡して、 "now"以外に初期化することができます。 –

関連する問題