2013-05-11 13 views
5

Dopo varie ricerche, non sono stato in grado di trovare quello che sto cercando. Sto usando jquery datepicker per restituire una stringa di data che assomiglia a Day, Month Date, YYYY e sto cercando una libreria o un metodo che lo prenderà e lo trasformerò in the second Tuesday of the month o the fourth Thursday of the month. Finora sembra che jquery's prettyDate e EasyDate non abbiano la funzionalità che sto cercando, e mi piacerebbe molto evitare di farlo a mano!Data una data (gg/aaaa) determinare se è "il terzo martedì" del mese ecc.

Grazie, Alex

+3

ho usato solo per le cose di base, quindi non sono sicuro che risolverà tutte le tue esigenze, ma potresti controllare: http://momentjs.com/ –

+0

non sembra che abbia la funzionalità di "nth mon/tues/etc del mese " –

+0

Vedi questo domanda: http://stackoverflow.com/a/16467099/592253 – Xotic750

risposta

0

Ecco uno script che ho usato di nuovo nel corso della giornata, se momentjs non funziona per voi (anche se penso davvero momentjs è una soluzione migliore).

/* 
Parameters: 
index: n'th occurrence of the specified day 
day: daynumber - javascript way where sunday is 0 and is saturday is 6 
month: javascript way which is 0-11 [optional - defaults to current] 
year: Full year - four digits [optional - defaults to current] 
*/ 
function getNthDayOfMonth(index,day,month,year){ 
// Create date object 
var date = new Date(); 
// Set to first day of month 
date.setDate(1); 
// If supplied - set the month 
if(month!==''&&month!==undefined){ 
    // Set month 
    date.setMonth(month); 
}else{ 
    month = date.getMonth(); 
} 
// If supplied - set the year 
if(year!==''&&year!==undefined){ 
    // Set year 
    date.setFullYear(year); 
}else{ 
    year = date.getFullYear(); 
} 
// Find daynumber 
firstDay = date.getDay(); 
// Find first friday. 
while(date.getDay()!=day){  
    date.setDate(date.getDate()+1) ; 
} 
switch(index){ 
    case 2: 
     date.setDate(date.getDate()+7);   
    break; 
    case 3: 
     date.setDate(date.getDate()+14);     
    break; 
    case 4: 
     date.setDate(date.getDate()+21); 
    break; 
    case 5: 
     date.setDate(date.getDate()+28); 
     if(date.getMonth()!==month){ 
      date = null; 
     } 
    break; 
} 
return date; 
} 
0

In Date.js si dispone di alcune funzioni utili come moveToFirstDayOfMonth e getWeek.

Quindi è possibile ottenere la settimana del primo giorno del mese e sottrarre quei due per ottenere la settimana del mese.

Ecco un semplice esempio che utilizza Date.js e va fuori di oggi ma è possibile passare nella propria data. Non ho molta familiarità con altre librerie di Date, ma penso che tu possa ricreare qualcosa di simile qui sotto.

function dayOfWeekToday(){ 
    //Get the total weeks on the year from this day 
    var totalWeek = Date.today().getWeek(); 

    //Get the first day of this month 
    var firstOfMonth = Date.today().moveToFirstDayOfMonth(); 

    //Get the total weeks form the first day of the month 
    var weeksAtStartOfMonth = firstOfMonth.getWeek(); 
    //Get the week of the month 
    var week = totalWeek - weeksAtStartOfMonth; 

    //Get the day(0-6) of today and the first day 
    var today = Date.today().getDay(); 
    var firstDay = firstOfMonth.getDay(); 

    //If today is before the firstDay then the week will be off by one 
    if(today < firstDay){ 
     week--; 
    } 
    //Get the day form of the string 
    var day = Date.today().toString("dddd"); 

    //Translate from a number to a string 
    var str = "first"; 
    if(week === 1){ 
      str = "second"; 
    }else if(week === 2){ 
     str = "third"; 
    }else if(week === 3){ 
     str = "fourth"; 
    }else if (week === 4){ 
     str = "fifth"; 
    } 
    //Result 
    return "The "+str+" "+day+" of the month"; 
} 
0

ho scritto qualche semplice logica che funziona solo a ritroso dal valore di data:

function calculateNthDate(startDateDay, startDateValue){ 
    var day = startDateDay; 
    var date = startDateValue; 
    var i = 0; 

    while(date >= 0){ 

     if (date > 0){ 
      i++; 
     } 
     date = date - 7; 
    } 
    var string = "The "+i+'th '+day+" of the month."; 
} 

edit: il th dovrà essere personalizzato per nd, st e rd.

7

Non è necessaria una libreria di date: è sufficiente prendere la data, dividere per 7 e arrotondare per eccesso.

//format: Day, Month Date, YYYY 
var ordinals = ["", "first", "second", "third", "fourth", "fifth"]; 
var date = "Friday, May 10, 2013"; 
var tokens = date.split(/[ ,]/); 
// tokens = ["Friday", "", "May", "10", "", "2013"]; 
console.log("The " + ordinals[Math.ceil(tokens[3]/7)] + " " + tokens[0] + " of the month"); 
0
function nthDay(D){ 
    var nth= ['First', 'Second', 'Third', 'Fourth', 'Fifth'], 
    dayNames= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 
    'Thursday', 'Friday', 'Saturday'], 
    monthNames= ['January', 'February', 'March', 'April', 'May', 'June', 
    'July', 'August', 'September', 'October', 'November', 'December']; 

    return nth[Math.floor(D.getDate()/7)]+' '+ 
    dayNames[D.getDay()]+' of '+monthNames[D.getMonth()]; 
} 

nthDay (new Date (2013, 4, 10))

/* valore restituito: secondo Venerdì di maggio */

+0

Questo fallisce. http://jsfiddle.net/7pd3vebc/ Questo è il primo lunedì di settembre. Usa ceil e lascia il primo membro dell'array blank-> http://jsfiddle.net/xka6hab9/ – ssaltman

Problemi correlati