Created
March 12, 2015 16:10
-
-
Save bbrown/8d60e50045280191678c to your computer and use it in GitHub Desktop.
Angular directive and service that validates that a particular date is on the third Friday of the month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("theModuleName").directive("mustBeThirdFriday", function(timeService) | |
{ | |
return { | |
require: "ngModel", | |
link: function(scope, element, attrs, ngModel) | |
{ | |
ngModel.$validators.mustBeThirdFriday = function(modelValue) | |
{ | |
return timeService.isThirdFriday(new Date(modelValue + " 0:00")); | |
}; | |
} | |
}; | |
}); | |
(function() | |
{ | |
"use strict"; | |
angular.module("theModuleName").factory("timeService", timeService); | |
timeService.$inject = []; | |
function timeService() | |
{ | |
var service = { | |
getThirdFriday: getThirdFriday, | |
isDst: isDst, | |
isExchangeOpen: isExchangeOpen, | |
isThirdFriday: isThirdFriday | |
}; | |
return service; | |
function getThirdFriday(year, month) | |
{ | |
var thirdFriday = new Date(year, month, 1); | |
thirdFriday.setDate(thirdFriday.getDate() + (12 - thirdFriday.getDay()) % 7); | |
thirdFriday.setDate(thirdFriday.getDate() + 14); | |
return thirdFriday; | |
} | |
function isDst() | |
{ | |
var now = new Date(); | |
var year = now.getYear(); | |
var secondSundayInMarch = new Date(year, 2, 7); | |
secondSundayInMarch.setDate(7 + (7 - secondSundayInMarch.getDay())); | |
var firstSundayInNovember = new Date(year, 10, 7); | |
firstSundayInNovember.setDate(7 - firstSundayInNovember.getDay()); | |
return (now <= firstSundayInNovember && now >= secondSundayInMarch); | |
} | |
function isExchangeOpen() | |
{ | |
var now = new Date(); | |
var inDst = isDst(); | |
var open, close; | |
if (inDst) | |
{ | |
open = new Date(); | |
open.setHours(6); | |
open.setMinutes(30); | |
close = new Date(); | |
close.setHours(13); | |
close.setMinutes(0); | |
return (now > open && now < close); | |
} | |
else | |
{ | |
open = new Date(); | |
open.setHours(7); | |
open.setMinutes(30); | |
close = new Date(); | |
close.setHours(14); | |
close.setMinutes(0); | |
return (now > open && now < close); | |
} | |
} | |
function isThirdFriday(dateInQuestion) | |
{ | |
if (dateInQuestion instanceof Date) | |
{ | |
var thirdFriday = getThirdFriday(dateInQuestion.getFullYear(), dateInQuestion.getMonth()); | |
return (dateInQuestion.getDate() === thirdFriday.getDate()); | |
} | |
return false; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment