Working with Date() Methods
  • Hi,

    I'm trying to figure out how to use JavaScript date methods on a return value of fieldType _date

    For example using any of the JavaScript functions 

    - getDate()
    - getDay()
    - getFullYear()
    - etc.

     on apsdb.creationTime, returns "errorDetail": "TypeError: Cannot find function getDate()/ getDay()/ ... in object 2014-09-11T12:32:54:+0000 ..."

    The methods seem to be available in general 'cause e.g

    var someDate = new Date()
    someDate().getHours() 

    works fine but the returned _date field from the request causes the mentioned error.

    Any workarround or steps I'm missing here?

    Regards Daniel


  • For more details,

    I have a field birthday in my apsdb.user schema of fieldType date for sure and I want to calculate the age depending on the current date
  • Hello Daniel,

    This is a known bug on fields of type date. We are working on it and it should be resolved soon.

    In the meantime, I suggest that you use the following function (pass it the value of the date field, it returns the corresponding JavaScript Date object:

    function toDate(dStr) {
    // Try to generate a date with the parameter. If it works, return it as is
    var date = new Date(dStr);
    if (date != "Invalid Date") {
    return date;
    }
    // If for some reason parsing the date failed, tried to parse it "manually" 
    // assuming it is in UTC format (GMT zero)
    var tSeperatorIndex = dStr.indexOf("T");
    if (tSeperatorIndex > -1) {
    var dayOfYearStr = dStr.substring(0, tSeperatorIndex).replace(/-/g, "/");
    var timeOfDayStr = dStr.substring(tSeperatorIndex + 1, dStr.length -1);
    var timeAndZone = timeOfDayStr.split("+");
    date = new Date(dayOfYearStr);
    if (timeOfDayStr.indexOf(":") > -1) {
    var hourMinSec = timeAndZone[0].split(":");
    date.setHours(hourMinSec[0]);
    date.setMinutes(hourMinSec[1]);
    date.setSeconds(hourMinSec[2]);
    }
    return date;
    }else {
    // If not in UTC format, return current date
    return new Date().getTime();
    }
    };
  • Hi Karim,

    thanks for the reply the function is working but as I like simplicity I did some research on the actual error messages I got from the Script and it seems that the JavaScript function new Date(<someDate>) didn't like the format of my input and I fiddled around a bit with following result:

    The idea was converting 
    1977-11-15T00:00:00+0000 ( which I got from the server)
    to 
    1977-11-15T00:00:00.000Z ( which conforms with new Date() method)

    // Get the birthday from the response
    var birthday = response.result.users[user].birthday[0]

    // Replace the '+' with '.'
    birthday = birthday.replace("+", ".")

    // Replace the last '0' by 'Z'
    birthday = birthday.substring(0, birthday.length - 1) + 'Z'

    // Create the date
    var date = new Date(birthday)

    Now I'm able to call all Date methods, if there's something wrong in my logic feel free to correct it but atm it's working fine for me

    Regards Daniel



  • Hi Daniel,

    As long as it is working fine with you, you should keep it as simple as possible:)


    Karim

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!