Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a date (m/d/yyyy) determine if its "the third tuesday" of the month etc [duplicate]

After several searches, I haven't been able to find what I'm looking for. Im using jquery datepicker to return a date string that looks like Day, Month Date, YYYY, and I am looking for a library or some method that will take that and turn it into the second Tuesday of the month, or the fourth Thursday of the month. So far it seems like jquery's prettyDate and EasyDate dont have the functionality I'm looking for, and I would love to avoid doing this by hand!

Thanks, Alex

like image 258
Alex Neigher Avatar asked May 11 '13 01:05

Alex Neigher


2 Answers

You don't need a date library - just take the date, divide by 7 and round up.

//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");
like image 155
Dennis Avatar answered Oct 22 '22 18:10

Dennis


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))

/* returned value: Second Friday of May */

like image 31
kennebec Avatar answered Oct 22 '22 19:10

kennebec