Leap year checker in javascript - javascript

I'm trying to put a leap year checker in my Age calculator. It worked for sometime and now it outputs "This is a leap year" every time I select a date. What am I doing wrong?? Thank you!
var year;
function ageCalculator() {
var userinput = document.getElementById("DOB").value;
var dob = new Date(userinput);
if (userinput == null || userinput == '') {
document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
} else {
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();
calYear();
//now calculate the age of the user
var age = Math.abs(year - 1970);
//display the calculated age
return document.getElementById("result").innerHTML =
"Age is: " + age + " years. ";
}
}
function calYear() {
var yr = year;
var yr = document.getElementsByName("year");
if (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0)) {
window.alert("This is not leap!");
} else {
window.alert("This is a leap!");
}
}
<input type="text" id="DOB" />
<button type="button" onclick="ageCalculator()">Calculate</button>
<span id="message"></span><br/>
<span id="result"></span>

document.getElementsByName("year") is a Collection not an Element value! Also, Element.value returns always a string - and it's your task as a developer to convert strings (or know typecasting by heart) to an integer or a float as necessary.
Don't put specifics into a function (like i.e: document.getElementById etc) make it reusable!
Avoid using variables that end up polluting the global Window scope (var year). At some point try to console.log(window.year) and you'll see why.
Don't call a function calYear() ? if it returns a string. Instead call it properly: isLeap() and do what it says! Return a Boolean value.
Only at the point you use/call that function in your UI - decide there the appropriate string you want to show. Don't limit yourself.
Be always careful while using new Date(someUserInput) where someUserInput might be an invalid ISO8601 string. You can easily get false positives.
PS: Date.now() - dob.getTime() cannot by any means be what your comment says: "calculate month difference" that's absolutely not true - Be careful when writing your comments and when naming your functions.
/**
* #param {integer} year
* #return {boolean} - True if year is leap year
*/
const isLeap = (year) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
/**
* #param {string} DOBString - A valid ISO8601 dateString
* #return {integer} - Age
*/
const calcAge = (DOBString) => {
const UTC_DOB = new Date(DOBString);
if (!DOBString || isNaN(UTC_DOB)) return;
const UTC_today = new Date();
const m = UTC_today.getUTCMonth() - UTC_DOB.getUTCMonth(); // Month diff
let age = UTC_today.getUTCFullYear() - UTC_DOB.getUTCFullYear();
if (m < 0 || (m === 0 && UTC_today.getUTCDate() < UTC_DOB.getUTCDate())) age--;
return age;
};
// UI/Page/View specific scripts:
const EL_DOB = document.querySelector("#DOB");
const EL_result = document.querySelector("#result");
const EL_leap = document.querySelector("#leap");
const UI_showAgeResult = () => {
const age = calcAge(EL_DOB.value);
const UTC_DOB = new Date(EL_DOB.value);
const is_leap = isLeap(UTC_DOB.getUTCFullYear());
EL_result.textContent = age ? age : "Insert a valid date";
EL_leap.textContent = is_leap ? "Born on a leap year! so cool" : "";
};
// Do it on input Event
EL_DOB.addEventListener("input", UI_showAgeResult);
// And if needed on page init
UI_showAgeResult();
<input id="DOB" type="text" value="2000-02-29"/>
<span id="result"></span><br>
<span id="leap"></span>
Important:
notice that if user Date Of Birth is say "2000-02-29", and say today's date is "2001-02-28" the result age will be arguably-correctly 0.
Only if date is "2001-03-01" will result as 1 y of age.
Kudos:
Slightly modified #naveen answer's code for calculating dates diff → age.

I suggest you look at this version
Calculate age given the birth date in the format YYYYMMDD
To fix YOUR code we need to pass the year to the function and not try to use a non-existing year field
If there was such a field, you could access its value with document.querySelector("[name=year]").value since document.getElementsByName is a collection and the value would be document.getElementsByName("year")[0].value
So what did I do?
passed the DOB year to the leap function
calculated the age from the year of the date difference
I tested with 2000-02-29 and 2001-02-28
NOTE the script ALSO works with 02/29/2016 but not with 29/02/2016 (European format)
var year;
function ageCalculator() {
var userinput = document.getElementById("DOB").value;
var dob = new Date(userinput);
if (isNaN(dob)) {
document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
} else {
//extract year from date
var year = dob.getFullYear();
console.log(year)
testLeap(year);
//calculate difference from current date in milliseconds
var date_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(date_diff);
//now calculate the age of the user
var age = Math.abs(1970 - age_dt.getFullYear());
//display the calculated age
return document.getElementById("result").innerHTML =
"Age is: " + age + " years. ";
}
}
function testLeap(yr) {
const isLeap = (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0))
alert(isLeap ? "This is a leap!" : "This is not a leap!");
}
<input type="text" id="DOB" />
<button type="button" onclick="ageCalculator()">Calculate</button>
<span id="message"></span><br/>
<span id="result"></span>

If you only need a function to check if the input year is a leap year or not, you can use Date object:
function isLeapYear(year) {
// year, monthIndex, day
return new Date(year, 1, 29).getFullYear() === year;
}

this is function to check if this year is leap year
function isLeapYear(year){
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
isLeapYear(2000) //true
isLeapYear(2001) // false
EDIT:
so the problem is in document.getElementsByName("year")
i think you should have a HTML named year which is have the year value
<input type="number" name="year" />

Here's another Method I can make sure it will works
function ageCalculator() {
var userinput = document.getElementById("DOB").value;
var dob = new Date(userinput);
if (userinput == null || userinput == '') {
document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
} else {
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();
calYear(year);
//now calculate the age of the user
var age = Math.abs(year - 1970);
//display the calculated age
return document.getElementById("result").innerHTML =
"Age is: " + age + " years. ";
}
}
function calYear(yr) {
if (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0)) {
window.alert("This is not leap!");
} else {
window.alert("This is a leap!");
}
}
Try updating the callYear if condition assuming
yr> 1000 and yr < 8000
function calYear(yr) {
if ((yr % 4 === 0 && yr % 100 !== 0) || yr % 400 === 0 ) {
window.alert("This is not leap!");
} else {
window.alert("This is a leap!");
}
}

Related

How to get javascript to throw error on bad date input? [duplicate]

I'm trying to test to make sure a date is valid in the sense that if someone enters 2/30/2011 then it should be wrong.
How can I do this with any date?
One simple way to validate a date string is to convert to a date object and test that, e.g.
// Expect input as d/m/y
function isValidDate(s) {
var bits = s.split('/');
var d = new Date(bits[2], bits[1] - 1, bits[0]);
return d && (d.getMonth() + 1) == bits[1];
}
['0/10/2017','29/2/2016','01/02'].forEach(function(s) {
console.log(s + ' : ' + isValidDate(s))
})
When testing a Date this way, only the month needs to be tested since if the date is out of range, the month will change. Same if the month is out of range. Any year is valid.
You can also test the bits of the date string:
function isValidDate2(s) {
var bits = s.split('/');
var y = bits[2],
m = bits[1],
d = bits[0];
// Assume not leap year by default (note zero index for Jan)
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// If evenly divisible by 4 and not evenly divisible by 100,
// or is evenly divisible by 400, then a leap year
if ((!(y % 4) && y % 100) || !(y % 400)) {
daysInMonth[1] = 29;
}
return !(/\D/.test(String(d))) && d > 0 && d <= daysInMonth[--m]
}
['0/10/2017','29/2/2016','01/02'].forEach(function(s) {
console.log(s + ' : ' + isValidDate2(s))
})
Does first function isValidDate(s) proposed by RobG will work for input string '1/2/'?
I think NOT, because the YEAR is not validated ;(
My proposition is to use improved version of this function:
//input in ISO format: yyyy-MM-dd
function DatePicker_IsValidDate(input) {
var bits = input.split('-');
var d = new Date(bits[0], bits[1] - 1, bits[2]);
return d.getFullYear() == bits[0] && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[2]);
}
I recommend to use moment.js. Only providing date to moment will validate it, no need to pass the dateFormat.
var date = moment("2016-10-19");
And then date.isValid() gives desired result.
Se post HERE
This solution does not address obvious date validations such as making sure date parts are integers or that date parts comply with obvious validation checks such as the day being greater than 0 and less than 32. This solution assumes that you already have all three date parts (year, month, day) and that each already passes obvious validations. Given these assumptions this method should work for simply checking if the date exists.
For example February 29, 2009 is not a real date but February 29, 2008 is. When you create a new Date object such as February 29, 2009 look what happens (Remember that months start at zero in JavaScript):
console.log(new Date(2009, 1, 29));
The above line outputs: Sun Mar 01 2009 00:00:00 GMT-0800 (PST)
Notice how the date simply gets rolled to the first day of the next month. Assuming you have the other, obvious validations in place, this information can be used to determine if a date is real with the following function (This function allows for non-zero based months for a more convenient input):
var isActualDate = function (month, day, year) {
var tempDate = new Date(year, --month, day);
return month === tempDate.getMonth();
};
This isn't a complete solution and doesn't take i18n into account but it could be made more robust.
var isDate_ = function(input) {
var status = false;
if (!input || input.length <= 0) {
status = false;
} else {
var result = new Date(input);
if (result == 'Invalid Date') {
status = false;
} else {
status = true;
}
}
return status;
}
this function returns bool value of whether the input given is a valid date or not. ex:
if(isDate_(var_date)) {
// statements if the date is valid
} else {
// statements if not valid
}
I just do a remake of RobG solution
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
var isLeap = new Date(theYear,1,29).getDate() == 29;
if (isLeap) {
daysInMonth[1] = 29;
}
return theDay <= daysInMonth[--theMonth]
This is ES6 (with let declaration).
function checkExistingDate(year, month, day){ // year, month and day should be numbers
// months are intended from 1 to 12
let months31 = [1,3,5,7,8,10,12]; // months with 31 days
let months30 = [4,6,9,11]; // months with 30 days
let months28 = [2]; // the only month with 28 days (29 if year isLeap)
let isLeap = ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
let valid = (months31.indexOf(month)!==-1 && day <= 31) || (months30.indexOf(month)!==-1 && day <= 30) || (months28.indexOf(month)!==-1 && day <= 28) || (months28.indexOf(month)!==-1 && day <= 29 && isLeap);
return valid; // it returns true or false
}
In this case I've intended months from 1 to 12. If you prefer or use the 0-11 based model, you can just change the arrays with:
let months31 = [0,2,4,6,7,9,11];
let months30 = [3,5,8,10];
let months28 = [1];
If your date is in form dd/mm/yyyy than you can take off day, month and year function parameters, and do this to retrieve them:
let arrayWithDayMonthYear = myDateInString.split('/');
let year = parseInt(arrayWithDayMonthYear[2]);
let month = parseInt(arrayWithDayMonthYear[1]);
let day = parseInt(arrayWithDayMonthYear[0]);
My function returns true if is a valid date otherwise returns false :D
function isDate (day, month, year){
if(day == 0 ){
return false;
}
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if(day > 31)
return false;
return true;
case 2:
if (year % 4 == 0)
if(day > 29){
return false;
}
else{
return true;
}
if(day > 28){
return false;
}
return true;
case 4: case 6: case 9: case 11:
if(day > 30){
return false;
}
return true;
default:
return false;
}
}
console.log(isDate(30, 5, 2017));
console.log(isDate(29, 2, 2016));
console.log(isDate(29, 2, 2015));
It's unfortunate that it seems JavaScript has no simple way to validate a date string to these days. This is the simplest way I can think of to parse dates in the format "m/d/yyyy" in modern browsers (that's why it doesn't specify the radix to parseInt, since it should be 10 since ES5):
const dateValidationRegex = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
function isValidDate(strDate) {
if (!dateValidationRegex.test(strDate)) return false;
const [m, d, y] = strDate.split('/').map(n => parseInt(n));
return m === new Date(y, m - 1, d).getMonth() + 1;
}
['10/30/2000abc', '10/30/2000', '1/1/1900', '02/30/2000', '1/1/1/4'].forEach(d => {
console.log(d, isValidDate(d));
});
Hi Please find the answer below.this is done by validating the date newly created
var year=2019;
var month=2;
var date=31;
var d = new Date(year, month - 1, date);
if (d.getFullYear() != year
|| d.getMonth() != (month - 1)
|| d.getDate() != date) {
alert("invalid date");
return false;
}
function isValidDate(year, month, day) {
var d = new Date(year, month - 1, day, 0, 0, 0, 0);
return (!isNaN(d) && (d.getDate() == day && d.getMonth() + 1 == month && d.getYear() == year));
}

Date validator in javascript

I have few months java experience, but now I am trying to write my first javascript to validate a date input. I am not sure what is wrong in the code.
<label for="Start-date" class="label">Available start date</label>
<input type="text" onsubmit="isValidDate()" class="w-input" maxlength="256" name="Start-date" data-name="Start date" placeholder="dd/mm/yyyy" id="Start-date" th:value="${possibleStartDate}" />
<script>
var validDate = true;
function isValidDate(){
var dateString = document.getElementById("Start-date");
// First check for the pattern
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString)){
validDate = false;
return;
}
// Parse the date parts to integers
var parts = dateString.split("/");
var day = parseInt(parts[1], 10);
var month = parseInt(parts[0], 10);
var year = parseInt(parts[2], 10);
// Check the ranges of month and year
if(year < 1000 || year > 3000 || month == 0 || month > 12){
validDate = false;
return;
}
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
monthLength[1] = 29;
}
// Check the range of the day
if (!(day > 0 && day <= monthLength[month - 1])){
validDate = false;
return;
}else{
return;
}
};
if(!validDate){
alert("Invalid date!");
}
</script>
Also what is the better alternative to alert ? Something like a red small message under the textfield that the input is incorrect when submit button is pressed. I know it is better to have separate file for the javascript, but I am not sure where in the html to put the tags to link the file.
There were quite a few things which needed a little tweak:
var dateString = document.getElementById("Start-date") should be var dateString = document.getElementById("Start-date").value;
var day = parseInt(parts[1], 10); should be var day = parseInt(parts[0], 10); and var month = parseInt(parts[0], 10); should be var month = parseInt(parts[1], 10); to support dd/mm/yyyy
Inputs don't fire onsubmit unless they are a part of a form you can try onblur instead
I have wrapped the input with div with children indicating success/error to show the message better. (The messages are hidden shown using classes on the root div and supporting styles)
I have spearated out the validation of a date string from DOM manipulation so you can reuse the validation across multiple places
NOTES:
You may be better off setting the input type as date (the browser will open up a date picker if it supports it)
You can also optionally use libraries like momentjs to make common date operations a little easier.
/**
* Function which takes a string as an input and validates if it
* is a date in the dd/mm/yyyy format
* #param {string} dateString - The string representation of the date to validate
8 #returns {boolean} value indicating if the date is valid (true) or not (false)
*/
function isValidDate(dateString) {
var validDate = true;
// First check for the pattern
if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString)) {
return false;
}
// Parse the date parts to integers
var parts = dateString.split("/");
var day = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10);
var year = parseInt(parts[2], 10);
if(isNaN(day) || isNaN(month) || isNaN(year)){
return false;
}
// Check the ranges of month and year
if (year < 1000 || year > 3000 || month < 1 || month > 12) {
return false;
}
var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Adjust for leap years
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
monthLength[1] = 29;
}
// Check the range of the day
if (!(day > 0 && day <= monthLength[month - 1])) {
return false;
}
return true;
};
/**
* Function which executes each time the input loses focus (onblur)
*/
function validateDate() {
// Get a reference to the container
var container = document.querySelector('.date');
// Clear stale error/success messages
container.className = container.className.replace('success', '');
container.className = container.className.replace('error', '');
// Get current value of input
var dateString = document.getElementById("Start-date").value;
// Test if the string is a valid date
var isValid = isValidDate(dateString);
// Update classess of container to show success/error
if (!isValid) {
container.className += ' error';
} else {
container.className += ' success';
}
}
div.date .error {
display: none;
background: red;
color: white;
}
div.date .success {
display: none;
background: darkgreen;
color: white;
}
div.date.success .success {
display: block;
}
div.date.error .error {
display: block;
}
<div class="date">
<label for="Start-date" class="label">Available start date</label>
<input type="text" onblur="validateDate()" class="w-input" maxlength="256" name="Start-date" data-name="Start date" placeholder="dd/mm/yyyy" id="Start-date" th:value="${possibleStartDate}" />
<div class="error">Please enter a valid date</div>
<div class="success">Date is valid</div>
</div>
If you're using the EcmaScript Date Time String Format, you can use the Date constructor. (Info
var dateString = document.getElementById("Start-date").value;
var validDate = !(isNaN( new Date(dateString) )
HTML5 also supports a type="date" in the <input> tag. This will prevent the browser from submitting the form if the date is invalid.
Also, I think you meant
var dateString = document.getElementById("Start-date").value;
Instead of
var dateString = document.getElementById("Start-date");
Because document.getElementById("Start-date") is an object.

How can I convert this function into Angular directive

How can I convert this function into an AngularJS directive?
I want to make it validate user input in real time. But even when I'm trying to use it as regular function in controller I'm getting an error:
"Error: pesel.substring is not a function"
function decode(pesel)
{
var year=parseInt(pesel.substring(0,2),10);
var month = parseInt(pesel.substring(2,4),10)-1;
var day = parseInt(pesel.substring(4,6),10);
if(month>80) {
year = year + 1800;
month = month - 80;
}
else if(month > 60) {
year = year + 2200;
month = month - 60;
}
else if (month > 40) {
year = year + 2100;
month = month - 40;
}
else if (month > 20) {
year = year + 2000;
month = month - 20;
}
else {
year += 1900;
}
var birthday=new Date();
birthday.setFullYear(year, month, day);
var weights = [9,7,3,1,9,7,3,1,9,7];
var sum = 0
for(var i=0;i<weights.length;i++) {
sum+=(parseInt(pesel.substring(i,i+1),10) * weights[i]);
}
sum=sum % 10;
var valid=(sum===parseInt(pesel.substring(10,11),10));
//sex
if(parseInt(pesel.substring(9,10),10) % 2 === 1) {
var sex='m';
} else {
var sex='f';
}
return {valid:valid,sex:sex,date:birthday};
}
As Abdo stated above the value you are passing to your function, pesel, probably isn't a string.
The first line in your function should be:
pesel = (pesel || '').toString();
This line makes sure you don't stringify falsey variables and will make sure pesel is in fact a string.
If you want to validate some input, consider as a better option to create a parser/formatter pair or a validator for that purpose.
Use formatter/parsers for retrieving birthday and sex values, and a validator to check the value.
Best

How do I validate yyyy-mm-dd hh:mm:ss format

I saw this fiddle for validating mm/dd/yyyy or mm-dd-yyyy but I would like to validate yyyy-mm-dd hh:mm:ss format also how do I ensure that today is lesser than from date with the yyyy-mm-dd hh:mm:ss format?.
this is how I have initiated my date time picker..
$("#startDate, #endDate").datetimepicker({ dateFormat: 'yyyy-mm-dd hh:mm:ss'});
Please do help me to get this done.
Thanks
The date format that you have specified is ISO 8601. Most modern browsers support Date parsing of this string format. So you can do something like this.
Javascript
var iso8601 = "2013-02-01 10:00:00",
userDate = new Date(iso8601),
today = new Date(),
dateTime,
date,
time,
value;
// check is valid date
if (isNaN(userDate)) {
alert("invalid userDate");
}
// check if userDate is before today
if (userDate.getDate() < today.getDate()) {
alert("userDate is in past");
}
// check the string specifically matches "yyyy-mm-dd hh:mm:ss" and is valid
function isGregorianLeapYear(year) {
return year % 400 === 0 || year % 100 !== 0 && year % 4 === 0;
}
function daysInGregorianMonth(year, month) {
var days;
if (month == 2) {
days = 28;
if (isGregorianLeapYear(year)) {
days += 1;
}
} else {
days = 31 - ((month - 1) % 7 % 2);
}
return days;
}
if (typeof iso8601 !== "string") {
alert("not an iso8601 string");
} else {
dateTime = iso8601.split(" ");
if (dateTime.length !== 2) {
alert("missing date or time element");
} else {
date = dateTime[0].split("-");
if (date.length !== 3) {
alert("incorrect number of date elements");
} else {
value = +date[0];
if (date[0].length !== 4 || value < -9999 || value > 9999) {
alert("year value is incorrect");
}
value = +date[1];
if (date[1].length !== 2 || value < 1 || value > 12) {
alert("month value is incorrect");
}
value = +date[2];
if (date[2].length !== 2 || value < 1 || value > daysInGregorianMonth(+date[0], +date[1])) {
alert("day value is incorrect");
}
}
time = dateTime[1].split(":");
if (time.length !== 3) {
alert("incorrect number of time elements");
} else {
value = +time[0];
if (time[0].length !== 2 || value < 0 || value > 23) {
alert("hour value is incorrect");
}
value = +time[1];
if (time[1].length !== 2 || value < 0 || value > 59) {
alert("minute value is incorrect");
}
value = +time[2];
if (time[2].length !== 2 || value < 0 || value > 59) {
alert("second value is incorrect");
}
}
}
}
console.log(userDate);
console.log(today);
jsFiddle
Checking yyyy-mm-dd hh:mm:ss strings against each other is pretty easy because they're already in order; you can forget about what base the numbers are in and simply do < or > as in string comparison. This may not work with other dates.
function compISOZDate(d1, d2) { // d1 is
if (d1 < d2) return -1; // smaller
if (d1 === d2) return 0; // the same
/* else */ return 1; // bigger
}
Validating dates is a bit more complicated, because the number of days in months can change. You can ignore this fact and just test for digits, but I prefer meeting half way, introducing upper limits.
function verifyMyDate(d) {
var re = /^\d{4}-(0[1-9]|1[0-2])-([0-2]\d|3[01]) (0\d|1[01]):[0-5]\d:[0-5]\d$/;
// yyyy - MM - dd hh : mm : ss
return re.test(d);
}
So for example using it
var d1 = '2013-10-07 11:58:26',
d2 = '2012-06-14 19:22:03';
// check
if (!verifyMyDate(d1)) throw new Error('Invalid date: ' + d1);
if (!verifyMyDate(d2)) throw new Error('Invalid date: ' + d2);
// compare
compISOZDate(d1, d2); // 1, d1 is bigger than d2
// also
compISOZDate(d2, d1); // -1
compISOZDate(d1, d1); // 0
Now all that is left is to get the value from your inputs.
Change your RegExp inside the ValidateDate function to below code
function ValidateDate(dtValue)
{
var dtRegex = new RegExp(/\b\d{4}[\/-]\d{1,2}[\/-]\b\d{1,2} (0\d|1[01]):[0-5]\d:[0-5]\d$\b/);
return dtRegex.test(dtValue);
}
try this and let me know, same way u can validate the hh:mm:ss also
For 24 hour variation, instead of AM/PM, use:
regex = new RegExp(^\d{4}-(0[1-9]|1[0-2])-([0-2]\d|3[01]) (0\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$);
This is mine I wrote and works fine :
function validateDate(dtValue) {
// Format expected dd/mm/aaaa or dd-mm-aaaa (French Date)
// Accept also d/m/aaaa or d-m-aaaa (ok for MySQL Database to have one number only for days and months)
// Before the insert into database I will convert to aaaa-mm-jj or aaaa-m-j
var dtRegex = new RegExp(/\b(0?[1-9]|([1-2]?[0-9]|3[0-1]))[\/-]([0]?[1-9]|1[0-2])[\/-]\b\d{4} ([0-1]?[0-9]|2[0-3]):[0-5]\d$\b/);
// Accept range => (01 to 31) (/ or -) (01 to 12) (/ or -) (4 numbers) space (00 to 23) : (00 to 59)
var bValidateDate= dtRegex.test(dtValue);
// Test if bValidateDate true, test and throw out (accepted from regex expression) :
// 30/02, 31/02, 31/04, 31/06, 31/09, 31/11
// 30-02, 31-02, 31-04, 31-06, 31-09, 31-11
// Get the 5 first characters
var sFebruary29= dtValue.substring(0, 5);
if (bValidateDate)
{
var aOfDateErrors= ["30/02", "31/02", "31/04", "31/06", "31/09", "31/11", "30-02", "31-02", "31-04", "31-06", "31-09", "31-11"];
if (aOfDateErrors.indexOf(sFebruary29) > -1)
{
bValidateDate= false;
}
}
// Then, if bValidateDate is still true (good format)
// check if the date is a leap year to accept 29/02 or 29-02
// And finally, my customer asked me to have a year between 2017 and now
if (bValidateDate)
{
// Get the year
var sYear= dtValue.substring(6, 10);
// Customer's constraints
var bYearCustomerOK= ((parseInt(sYear) >= 2017) && (parseInt(sYear) <= new Date().getFullYear()));
// I promise, this is the "last test'em all !"
var bFinalDate= (bYearCustomerOK) && (sYear % 400 === 0 || sYear % 100 !== 0 && sYear % 4 === 0) && ((sFebruary29 == "29/02") ||(sFebruary29 == "29-02"));
if (! bFinalDate)
{
bValidateDate= false;
}
}
return bValidateDate;
}
Please, let me know if you find a bad date :)

How can I calculate the number of years between two dates?

I want to get the number of years between two dates. I can get the number of days between these two days, but if I divide it by 365 the result is incorrect because some years have 366 days.
This is my code to get date difference:
var birthday = value;//format 01/02/1900
var dateParts = birthday.split("/");
var checkindate = new Date(dateParts[2], dateParts[0] - 1, dateParts[1]);
var now = new Date();
var difference = now - checkindate;
var days = difference / (1000*60*60*24);
var thisyear = new Date().getFullYear();
var birthyear = dateParts[2];
var number_of_long_years = 0;
for(var y=birthyear; y <= thisyear; y++){
if( (y % 4 == 0 && y % 100 == 0) || y % 400 == 0 ) {
number_of_long_years++;
}
}
The day count works perfectly. I am trying to do add the additional days when it is a 366-day year, and I'm doing something like this:
var years = ((days)*(thisyear-birthyear))
/((number_of_long_years*366) + ((thisyear-birthyear-number_of_long_years)*365) );
I'm getting the year count. Is this correct, or is there a better way to do this?
Sleek foundation javascript function.
function calculateAge(birthday) { // birthday is a date
var ageDifMs = Date.now() - birthday;
var ageDate = new Date(ageDifMs); // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
Probably not the answer you're looking for, but at 2.6kb, I would not try to reinvent the wheel and I'd use something like moment.js. Does not have any dependencies.
The diff method is probably what you want: http://momentjs.com/docs/#/displaying/difference/
Using pure javascript Date(), we can calculate the numbers of years like below
document.getElementById('getYearsBtn').addEventListener('click', function () {
var enteredDate = document.getElementById('sampleDate').value;
// Below one is the single line logic to calculate the no. of years...
var years = new Date(new Date() - new Date(enteredDate)).getFullYear() - 1970;
console.log(years);
});
<input type="text" id="sampleDate" value="1980/01/01">
<div>Format: yyyy-mm-dd or yyyy/mm/dd</div><br>
<button id="getYearsBtn">Calculate Years</button>
No for-each loop, no extra jQuery plugin needed... Just call the below function.. Got from Difference between two dates in years
function dateDiffInYears(dateold, datenew) {
var ynew = datenew.getFullYear();
var mnew = datenew.getMonth();
var dnew = datenew.getDate();
var yold = dateold.getFullYear();
var mold = dateold.getMonth();
var dold = dateold.getDate();
var diff = ynew - yold;
if (mold > mnew) diff--;
else {
if (mold == mnew) {
if (dold > dnew) diff--;
}
}
return diff;
}
I use the following for age calculation.
I named it gregorianAge() because this calculation gives exactly how we denote age using Gregorian calendar. i.e. Not counting the end year if month and day is before the month and day of the birth year.
/**
* Calculates human age in years given a birth day. Optionally ageAtDate
* can be provided to calculate age at a specific date
*
* #param string|Date Object birthDate
* #param string|Date Object ageAtDate optional
* #returns integer Age between birthday and a given date or today
*/
gregorianAge = function(birthDate, ageAtDate) {
// convert birthDate to date object if already not
if (Object.prototype.toString.call(birthDate) !== '[object Date]')
birthDate = new Date(birthDate);
// use today's date if ageAtDate is not provided
if (typeof ageAtDate == "undefined")
ageAtDate = new Date();
// convert ageAtDate to date object if already not
else if (Object.prototype.toString.call(ageAtDate) !== '[object Date]')
ageAtDate = new Date(ageAtDate);
// if conversion to date object fails return null
if (ageAtDate == null || birthDate == null)
return null;
var _m = ageAtDate.getMonth() - birthDate.getMonth();
// answer: ageAt year minus birth year less one (1) if month and day of
// ageAt year is before month and day of birth year
return (ageAtDate.getFullYear()) - birthDate.getFullYear()
- ((_m < 0 || (_m === 0 && ageAtDate.getDate() < birthDate.getDate()))?1:0)
}
<input type="text" id="birthDate" value="12 February 1982">
<div style="font-size: small; color: grey">Enter a date in an acceptable format e.g. 10 Dec 2001</div><br>
<button onClick='js:alert(gregorianAge(document.getElementById("birthDate").value))'>What's my age?</button>
Little out of date but here is a function you can use!
function calculateAge(birthMonth, birthDay, birthYear) {
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var calculatedAge = currentYear - birthYear;
if (currentMonth < birthMonth - 1) {
calculatedAge--;
}
if (birthMonth - 1 == currentMonth && currentDay < birthDay) {
calculatedAge--;
}
return calculatedAge;
}
var age = calculateAge(12, 8, 1993);
alert(age);
You can get the exact age using timesstamp:
const getAge = (dateOfBirth, dateToCalculate = new Date()) => {
const dob = new Date(dateOfBirth).getTime();
const dateToCompare = new Date(dateToCalculate).getTime();
const age = (dateToCompare - dob) / (365 * 24 * 60 * 60 * 1000);
return Math.floor(age);
};
let currentTime = new Date().getTime();
let birthDateTime= new Date(birthDate).getTime();
let difference = (currentTime - birthDateTime)
var ageInYears=difference/(1000*60*60*24*365)
Yep, moment.js is pretty good for this:
var moment = require('moment');
var startDate = new Date();
var endDate = new Date();
endDate.setDate(endDate.getFullYear() + 5); // Add 5 years to second date
console.log(moment.duration(endDate - startDate).years()); // This should returns 5
getYears(date1, date2) {
let years = new Date(date1).getFullYear() - new Date(date2).getFullYear();
let month = new Date(date1).getMonth() - new Date(date2).getMonth();
let dateDiff = new Date(date1).getDay() - new Date(date2).getDay();
if (dateDiff < 0) {
month -= 1;
}
if (month < 0) {
years -= 1;
}
return years;
}
for(var y=birthyear; y <= thisyear; y++){
if( (y % 4 == 0 && y % 100 == 0) || y % 400 == 0 ) {
days = days-366;
number_of_long_years++;
} else {
days=days-365;
}
year++;
}
can you try this way??
function getYearDiff(startDate, endDate) {
let yearDiff = endDate.getFullYear() - startDate.getFullYear();
if (startDate.getMonth() > endDate.getMonth()) {
yearDiff--;
} else if (startDate.getMonth() === endDate.getMonth()) {
if (startDate.getDate() > endDate.getDate()) {
yearDiff--;
} else if (startDate.getDate() === endDate.getDate()) {
if (startDate.getHours() > endDate.getHours()) {
yearDiff--;
} else if (startDate.getHours() === endDate.getHours()) {
if (startDate.getMinutes() > endDate.getMinutes()) {
yearDiff--;
}
}
}
}
return yearDiff;
}
alert(getYearDiff(firstDate, secondDate));
getAge(month, day, year) {
let yearNow = new Date().getFullYear();
let monthNow = new Date().getMonth() + 1;
let dayNow = new Date().getDate();
if (monthNow === month && dayNow < day || monthNow < month) {
return yearNow - year - 1;
} else {
return yearNow - year;
}
}
If you are using moment
/**
* Convert date of birth into age
* param {string} dateOfBirth - date of birth
* param {string} dateToCalculate - date to compare
* returns {number} - age
*/
function getAge(dateOfBirth, dateToCalculate) {
const dob = moment(dateOfBirth);
return moment(dateToCalculate).diff(dob, 'years');
};
If you want to calculate the years and keep the remainder of the time left for further calculations you can use this function most of the other answers discard the remaining time.
It returns the years and the remainder in milliseconds. This is useful if you want to calculate the time (days or minutes) left after you calculate the years.
The function works by first calculating the difference in years directly using *date.getFullYear()*.
Then it checks if the last year between the two dates is up to a full year by setting the two dates to the same year.
Eg:
oldDate= 1 July 2020,
newDate= 1 June 2022,
years =2020 -2022 =2
Now set old date to new date's year 2022
oldDate = 1 July, 2022
If the last year is not up to a full year then the year is subtracted by 1, the old date is set to the previous year and the interval from the previous year to the current date is calculated to give the remainder in milliseconds.
In the example since old date July 2022 is greater than June 2022 then it means a full year has not yet elapsed (from July 2021 to June 2022) therefore the year count is greater by 1. So years should be decreased by 1. And the actual year count from July 2020 to June 2022 is 1 year ,... months.
If the last year is a full year then the year count by *date.getFullYear()* is correct and the time that has elapsed from the current old date to new date is calculated as the remainder.
If old date= 1 April, 2020, new date = 1 June, 2022 and old date is set to April 2022 after calculating the year =2.
Eg: from April 2020 to June 2022 a duration of 2 years has passed with the remainder being the time from April 2022 to June 2022.
There are also checks for cases where the two dates are in the same year and if the user enters the dates in the wrong order the new Date is less recent than the old Date.
let getYearsAndRemainder = (newDate, oldDate) => {
let remainder = 0;
// get initial years between dates
let years = newDate.getFullYear() - oldDate.getFullYear();
if (years < 0) {// check to make sure the oldDate is the older of the two dates
console.warn('new date is lesser than old date in year difference')
years = 0;
} else {
// set the old date to the same year as new date
oldDate.setFullYear(newDate.getFullYear());
// check if the old date is less than new date in the same year
if (oldDate - newDate > 0) {
//if true, the old date is greater than the new date
// the last but one year between the two dates is not up to a year
if (years != 0) {// dates given in inputs are in the same year, no need to calculate years if the number of years is 0
console.log('Subtracting year');
//set the old year to the previous year
years--;
oldDate.setFullYear(oldDate.getFullYear() - 1);
}
}
}
//calculate the time difference between the old year and newDate.
remainder = newDate - oldDate;
if (remainder < 0) { //check for negative dates due to wrong inputs
console.warn('old date is greater than new Date');
console.log('new date', newDate, 'old date', oldDate);
}
return { years, remainder };
}
let old = new Date('2020-07-01');
console.log( getYearsAndRemainder(new Date(), old));
Date calculation work via the Julian day number. You have to take the first of January of the two years. Then you convert the Gregorian dates into Julian day numbers and after that you take just the difference.
Maybe my function can explain better how to do this in a simple way without loop, calculations and/or libs
function checkYearsDifference(birthDayDate){
var todayDate = new Date();
var thisMonth = todayDate.getMonth();
var thisYear = todayDate.getFullYear();
var thisDay = todayDate.getDate();
var monthBirthday = birthDayDate.getMonth();
var yearBirthday = birthDayDate.getFullYear();
var dayBirthday = birthDayDate.getDate();
//first just make the difference between years
var yearDifference = thisYear - yearBirthday;
//then check months
if (thisMonth == monthBirthday){
//if months are the same then check days
if (thisDay<dayBirthday){
//if today day is before birthday day
//then I have to remove 1 year
//(no birthday yet)
yearDifference = yearDifference -1;
}
//if not no action because year difference is ok
}
else {
if (thisMonth < monthBirthday) {
//if actual month is before birthday one
//then I have to remove 1 year
yearDifference = yearDifference -1;
}
//if not no action because year difference is ok
}
return yearDifference;
}
Bro, moment.js is awesome for this:
The diff method is what you want: http://momentjs.com/docs/#/displaying/difference/
The below function return array of years from the year to the current year.
const getYears = (from = 2017) => {
const diff = moment(new Date()).diff(new Date(`01/01/${from}`), 'years') ;
return [...Array(diff >= 0 ? diff + 1 : 0).keys()].map((num) => {
return from + num;
});
}
console.log(getYears(2016));
<script src="https://momentjs.com/downloads/moment.js"></script>
function dateDiffYearsOnly( dateNew,dateOld) {
function date2ymd(d){ w=new Date(d);return [w.getFullYear(),w.getMonth(),w.getDate()]}
function ymd2N(y){return (((y[0]<<4)+y[1])<<5)+y[2]} // or 60 and 60 // or 13 and 32 // or 25 and 40 //// with ...
function date2N(d){ return ymd2N(date2ymd(d))}
return (date2N(dateNew)-date2N(dateOld))>>9
}
test:
dateDiffYearsOnly(Date.now(),new Date(Date.now()-7*366*24*3600*1000));
dateDiffYearsOnly(Date.now(),new Date(Date.now()-7*365*24*3600*1000))
I went for the following very simple solution. It does not assume you were born in 1970 and it also takes into account the hour of the given birthday date.
function age(birthday) {
let now = new Date();
let year = now.getFullYear();
let years = year - birthday.getFullYear();
birthday = new Date(birthday.getTime()); // clone
birthday.setFullYear(year);
return now >= birthday ? years : years - 1;
}
This one Help you...
$("[id$=btnSubmit]").click(function () {
debugger
var SDate = $("[id$=txtStartDate]").val().split('-');
var Smonth = SDate[0];
var Sday = SDate[1];
var Syear = SDate[2];
// alert(Syear); alert(Sday); alert(Smonth);
var EDate = $("[id$=txtEndDate]").val().split('-');
var Emonth = EDate[0];
var Eday = EDate[1];
var Eyear = EDate[2];
var y = parseInt(Eyear) - parseInt(Syear);
var m, d;
if ((parseInt(Emonth) - parseInt(Smonth)) > 0) {
m = parseInt(Emonth) - parseInt(Smonth);
}
else {
m = parseInt(Emonth) + 12 - parseInt(Smonth);
y = y - 1;
}
if ((parseInt(Eday) - parseInt(Sday)) > 0) {
d = parseInt(Eday) - parseInt(Sday);
}
else {
d = parseInt(Eday) + 30 - parseInt(Sday);
m = m - 1;
}
// alert(y + " " + m + " " + d);
$("[id$=lblAge]").text("your age is " + y + "years " + m + "month " + d + "days");
return false;
});
if someone needs for interest calculation year in float format
function floatYearDiff(olddate, newdate) {
var new_y = newdate.getFullYear();
var old_y = olddate.getFullYear();
var diff_y = new_y - old_y;
var start_year = new Date(olddate);
var end_year = new Date(olddate);
start_year.setFullYear(new_y);
end_year.setFullYear(new_y+1);
if (start_year > newdate) {
start_year.setFullYear(new_y-1);
end_year.setFullYear(new_y);
diff_y--;
}
var diff = diff_y + (newdate - start_year)/(end_year - start_year);
return diff;
}

Categories

Resources