Javascript:距下一个生日的天数,从数组中对象的日期属性开始

标签 javascript object properties countdown

本质上我不明白的是如何获取对象的 Date 属性(生日 - 从 HTML 中的 Date 类型输入检索)并使用它向对象添加“距离下一个生日的天数”属性,例如示例:

var surnameField,firstNameField,birthdayField, phoneField, addressField, postField, emailField, groupField ;  //Declaring variables for the fields

var Contact = function(surname,firstName,date, phone , address , post, email, group){
    this.surname = surname ;
    this.firstName = firstName ;
    this.birthdayDate = new Date (date) ;
    this.phone = phone;
    this.address= address;
    this.email = email;
    this.post = post;
    this.group = group;

}



var contacts = [];

var getday = function() {

    for (var i= 0, j=contacts.length;i<j;i++){
       var a = contacts[i].birthdayDate.getDate();
        contacts[i].days = a;

    }
}





Contact.prototype.tableRow = function(){
    var tr = "<tr><td>" + this.surname + "</td><td>" + this.firstName + "</td><td>" + this.birthdayDate + "</td><td>" + this.phone + "</td><td>" + this.email +
            "</td><td>" + this.address + "</td><td>" + this.post + "</td><td>" + this.group + "</td></tr>";
    return tr;
}

var addContact = function(surnameField,firstNameField,birthdayField, phoneField, addressField, postField, emailField, groupField ){
        if(surnameField.value){
            a = new Contact(surnameField.value, firstNameField.value,birthdayField.value, phoneField.value, addressField.value, postField.value, emailField.value, groupField.value);
            contacts.push(a);
        }else{ alert("Please complete all fields")}

}

var clearUI = function(){
    var white = "#fff";
    surnameField.value = "";
    surnameField.style.backgroundColor = white;
    firstNameField.value = "";
    firstNameField.style.backgroundColor = white;
    birthdayField.value="";
    birthdayField.style.backgroundColor = white;
    phoneField.value = "";
    phoneField.style.backgroundcolor = white;
    addressField.value = "";
    addressField.style.backgroundcolor = white;
    postField.value = "";
    postField.style.backgroundcolor = white;
    emailField.value = "";
    emailField.style.backgroundcolor = white;
    groupField.value="";
    groupField.style.backgroundcolor = white;
}


var updateList = function(){
    var tableDiv = document.getElementById("table"),
        table = "<table border='2'><thead><th>  Surname  </th><th> First Name</th><th>Date of Birth</th><th>  Phone Number  </th><th>  Email Address </th><th>  Address  </th><th> Postcode  </th><th>Group</th></thead>";

    for (var i= 0, j=contacts.length;i<j;i++){
        var cntct = contacts[i];
        table += cntct.tableRow();
    }
    table +="</table>";
    tableDiv.innerHTML = table;
    //getday();
    saveContacts();

}

var add = function(){
    addContact(surnameField,firstNameField,birthdayField, phoneField, addressField, postField, emailField, groupField);
    clearUI();
    updateList();
};

var saveContacts = function(){
    var cntcts = JSON.stringify(contacts);
    if (cntcts !==""){
        localStorage.contacts = cntcts;
    }else{
        alert("Could not save contacts");
    }
}

var loadContacts = function(){
    var cntcts = "";
    if(localStorage.contacts !== undefined){
        cntcts = localStorage.contacts;
        contacts = JSON.parse(cntcts);
        var proto = new Contact();
        for (var i=0; i<contacts.length; i++){
            var cntct = contacts[i]
            cntct.__proto__ = proto;
        }
    }
}

var clearContacts = function(){
    contacts = [];
    updateList()

}

/*contacts.prototype.isDue = function(){
    var now = new Date();
    if(this.birthday > now){
        return false;
    } else {
        return true;
    }
};*/



window.onload = function(){
    loadContacts();
    updateList();
    surnameField = document.getElementById("surname");
    firstNameField = document.getElementById("firstName")
    birthdayField = document.getElementById("birthday");
    phoneField = document.getElementById("phone");
    addressField = document.getElementById("address");
    postField = document.getElementById("post");
    emailField = document.getElementById("email");
    groupField = document.getElementById("group");
    addButton = document.getElementById("addButton");
    addButton.onclick = add;
    delButton = document.getElementById("delButton");
    delButton.onclick = clearContacts;
    clearUI();
}

我试图做的是从日期对象中获取天/月值,并用它来计算到下一个生日的天数,但是我尝试过的一切似乎都不起作用。 getDay() 和 getMonth() 在控制台中工作,但在实现时我收到了“getDay 不在 .birthday 的 deinfed 方法”类型的错误。

因此,理想情况下,一旦修复,数组中的每个对象都应具有“daysTilBirthday”属性。

任何帮助将不胜感激。

已解决

我忘记添加检查以确保重新加载数据时,它被重新加载为日期,而不是字符串,例如

cntct.birthdayDate = new Date(cntct.birthdayDate);

无论如何,感谢您的帮助!

最佳答案

尝试birthday.getDate()

这是 fiddle :http://jsfiddle.net/ZJ6CN/

关于Javascript:距下一个生日的天数,从数组中对象的日期属性开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13548156/

相关文章:

JAVAFX:setMaxWidth() 和 maxWidthProperty().set() 之间的区别

ios - swift 。通过字符串名称引用实例变量

javascript - 创建自定义过滤器来更改标签 AngularJS 的属性

Python 使用多个文件从类创建对象

javascript - 使用传单和图层控件显示 Shapefile

c# - 在 JSON 属性名称中使用点表示法(例如 "payment.token")

java - 在线程中调用地理编码器构造函数时,连接崩溃

c# - C#中的 `Fields`和 `Properties`有什么区别?

javascript - 将日期插入 firebase 数据库中的新条目

javascript - 在 Vue 中实现 @keypress