2

I have an JavaScript array here. I need to compare the birthday value with a set date and update the record with a new key value.

var employees = [
    {
        "internalid":"1", 
        "name":"Abe Anderson", 
        "email":"aanderson@javascript.com", 
        "birthdate":"9/25/1974", 
        "supervisor":"3", 
        "2012 Revenue":"100000.00", 
        "2013 Revenue":"0.00"
    }
];

I wrote this here which works great,

for (var i = 0; i < employees.length; i++) {
    var cDate = new Date("2014/01/01");
    var newDate = cDate.getMonth()+1 + '/' + cDate.getDate() + '/' + cDate.getFullYear();
    var eBday = employees[i].birthdate;
}

I'm having a hard time writing the math to compare the two dates correctly. Can anyone help me? I need to calculate how many days left each person has until his or her birthday and update the JavaScript array. I'm stuck!

naveen
  • 53,448
  • 46
  • 161
  • 251
gx2g
  • 319
  • 5
  • 18
  • 1
    Try to use [momentJS](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjO_8Wp34bOAhWFbxQKHe4MCsgQFggeMAA&url=http%3A%2F%2Fmomentjs.com%2F&usg=AFQjCNGFVPw4mNHLQAflxUNJzaN0KZuxRw&sig2=hWo8Q741o6yY5yej8mjQ_A). – Zakaria Acharki Jul 22 '16 at 09:21
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Robby Cornelissen Jul 22 '16 at 09:21
  • May i ask why are you using `var cDate = new Date("2014/01/01");`, do you want to calculate from a given date including years ? Why not use the current date ? – DavidDomain Jul 22 '16 at 09:43
  • the problem is: assume that today is 1/1/2014, update each employee's record to indicate the number of days until their birthday. Store the resulting value in a new key on the employee object. – gx2g Jul 22 '16 at 15:43
  • I chose to do this but I get NaN back and I'm not sure what I'm doing to cause that. for (var i = 0; i < employees.length; i++){ var cDate = new Date("01/01/2014"); var eBday = employees[0].birthdate; var total_days = (eBday - cDate) / (1000 * 60 * 60 * 24); document.getElementById("birthDays").innerHTML = total_days; } – gx2g Jul 23 '16 at 00:16

2 Answers2

1

I'd recommend using momentJS. its the go to library for javascript dates handling.

in momentJS you can use moment.diff method: http://momentjs.com/docs/#/displaying/difference/

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

This guy answered how to make compare using momentJS in details:https://stackoverflow.com/a/22601120/6624456

Community
  • 1
  • 1
0

Try this.

var employees = [{
  "internalid": "1",
  "name": "Abe Anderson",
  "email": "aanderson@javascript.com",
  "birthdate": "9/25/1974",
  "supervisor": "3",
  "2012 Revenue": "100000.00",
  "2013 Revenue": "0.00"
}];

for (var i = 0; i < employees.length; i++) {
  employees[i].daysToBirthday = DaysToBirthdayFromToday(employees[i].birthdate);
}

console.log(employees);


function DaysToBirthdayFromToday(birthdayString) {
  "use strict";
  var currentYear = new Date().getFullYear();
  //get today midnight
  var today = new Date(currentYear, new Date().getMonth(), new Date().getDate());
  var birthdayParts = birthdayString.split("/");
  var yearBirthday = new Date(currentYear, birthdayParts[0] - 1, birthdayParts[1]);
  var timDiffInMilliSeconds = yearBirthday.getTime() - today.getTime();
  var timDiffInDays = timDiffInMilliSeconds / (1000 * 60 * 60 * 24);
  return timDiffInDays < 0 ? 0 : timDiffInDays; // set zero if past
}
naveen
  • 53,448
  • 46
  • 161
  • 251