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
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");
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 */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With