Created
August 1, 2017 19:03
-
-
Save Security2431/858f31851399aff6473419ce0f8c1607 to your computer and use it in GitHub Desktop.
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
// Today date | |
$(function () { | |
Date.prototype.addDays = function(days) { | |
this.setDate(this.getDate() + parseInt(days)); | |
return this; | |
}; | |
Date.prototype.format = function(format) { | |
// set default format if function argument not provided | |
format = format || 'YYYY-MM-DD hh:mm'; | |
var zeropad = function(number, length) { | |
number = number.toString(); | |
length = length || 2; | |
while(number.length < length) | |
number = '0' + number; | |
return number; | |
}, | |
// here you can define your formats | |
formats = { | |
YYYY: this.getFullYear(), | |
MM: zeropad(this.getMonth() + 1), | |
DD: zeropad(this.getDate()), | |
hh: zeropad(this.getHours()), | |
mm: zeropad(this.getMinutes()) | |
}, | |
pattern = '(' + Object.keys(formats).join(')|(') + ')'; | |
return format.replace(new RegExp(pattern, 'g'), function(match) { | |
return formats[match]; | |
}); | |
}; | |
var currentDate = new Date(), | |
$dates = $('[data-time]'), | |
$currentDate = $('.date__number'); | |
$currentDate.text(currentDate.format('DD.MM.YYYY')); | |
$dates.each(function() { | |
var $this = $(this), | |
addDate = $this.data('time'); | |
currentDate.addDays(addDate); | |
$this.text(currentDate.format('DD.MM.YYYY')); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment