Güncelleme Yaklaşım dönmek (döngü olmadan)
day()
işlevinden yararlanabilirsiniz.
function getPreviousWorkday(){
// Based on the current day, handle accordingly
switch(moment().day())
{
// If it is Monday (1),Saturday(6), or Sunday (0), Get the previous Friday (5)
// and ensure we are on the previous week
case 0:
case 1:
case 6:
return moment().subtract(6,'days').day(5);
// If it any other weekend, just return the previous day
default:
return moment().day(today - 1);
}
}
hangi can be seen here bunları aşağıda gösterildiği: o belirli günlerde dayalı önceki Cuma bulmak moment.js haftanın anki gününü belirlemek için izin verecek
Döngü Yaklaşım
Geçerli andan güne subtract()
işlevini kullanarak mevcut anı örneğinizden günler çıkartabilirsiniz.
function getPreviousWorkday(){
// Get today
var today = new moment().subtract(-1,'days');;
// If today isn't a weekend, continue iterating back until you hit a non-weekend
while([0,6].indexOf(today.day()) !== -1){
today = today.subtract(1, 'days');
}
// Return the non-weekend day
return today;
}
Aşağıda gösterildiği see an example of this in action here ve: olmayan bir haftasonu gününü ulaştı