Created
May 22, 2013 15:47
-
-
Save bitsprint/5628625 to your computer and use it in GitHub Desktop.
Date functions
This file contains hidden or 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
| Date.prototype.formatDDMMYYYY = function () { | |
| return ('0' + this.getDate()).slice(-2) + | |
| '/' + ('0' + (this.getMonth() + 1)).slice(-2) + | |
| '/' + this.getFullYear(); | |
| }; | |
| Date.prototype.formatDDMMYYYYWithHMS = function () { | |
| return ('0' + this.getDate()).slice(-2) + | |
| '/' + ('0' + (this.getMonth() + 1)).slice(-2) + | |
| '/' + this.getFullYear() + | |
| ' ' + ('0' + this.getHours()).slice(-2) + | |
| ':' + ('0' + this.getMinutes()).slice(-2) + | |
| ':' + ('0' + this.getSeconds()).slice(-2); | |
| }; | |
| function getDateParts(date, delim) { | |
| // TODO: Make this regex work with any delimiter | |
| // var regex = new RegExp(delim + "(\d+)/g"); | |
| var parts = date.match(/(\d+)/g); | |
| return parts; | |
| } | |
| function getDaysInMonth(year, month) { | |
| return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]; | |
| } | |
| function isLeapYear(year) { | |
| return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)); | |
| } | |
| function isValidDate(value, delim) { | |
| if (value === "" || value === null || value === undefined) | |
| return false; | |
| var dateParts = getDateParts(value, delim); | |
| if (dateParts === null || dateParts.length !== 3) | |
| return false; | |
| var day = parseInt(dateParts[0], 10), month = parseInt(dateParts[1], 10); | |
| return (dateParts[2].length == 4 && (month > 0 && month < 13) && (day > 0 && day <= getDaysInMonth(dateParts[2], month))); | |
| } | |
| function dateRangeContainsValue (date, start, end) { | |
| var startDate = parseDate(start); | |
| var endDate = parseDate(end); | |
| if (isNaN(startDate) || isNaN(endDate) || !isValidDate(date, '/')) { | |
| return false; | |
| } | |
| var thisDate = parseDate(date); | |
| return (thisDate >= startDate && thisDate <= endDate); | |
| } | |
| function dateRangeWithinBoundary (date, start, end) { | |
| var startDate = parseDate(start); | |
| var endDate = parseDate(end); | |
| if (isNaN(startDate) || isNaN(endDate) || !isValidDate(date, '/')) { | |
| return false; | |
| } | |
| var thisDate = parseDate(date); | |
| return (thisDate > startDate && thisDate < endDate); | |
| } | |
| function parseDate(input) { | |
| var parts = input.match(/(\d+)/g); | |
| return new Date(parts[2], parts[1] - 1, parts[0]); // months are 0-based | |
| } | |
| /* jQuery DateTime Utils */ | |
| jQuery.fn.isValidDate = function (delim) { | |
| return isValidDate($(this).val(), delim); | |
| }; | |
| jQuery.fn.getDateParts = function (delim) { | |
| return getDateParts($(this).val(), delim); | |
| }; | |
| jQuery.fn.dateRangeContainsValue = function (start, end) { | |
| return dateRangeContainsValue($(this).val(), start, end); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment