Looking for a way to have ‘age in days’ in a field … like the excel function Today-date… it would calculate the difference in days between a date field and Today’s date and be a dynamic field
Hi Natalie
if you want to calculate between 2 date filed .
(date1filedname-date2fieldname)/86400
Thanks! I want the field to be dynamic however, so if a specific date field has been filled out for 10/27/2016, on 10/28/2016 the value would be 1 day, and on 10/29/2016 the value would be 2 days.
Hello Natalie,
Regarding the question, is this only to make it show up on the page(which means not to save the calculated data), or are you trying to save the calculated data to use it in other process?
Thank you
Hi Yuzo, the calculation would be just to show up on the page, not to save the calculated data each day. Thanks!
Hello Natalie,
Regarding how to calculate the age between two dates(a specified date and today’s date), this is a sample program that is on the Japanese version of kintone developer network site(I translated the part that was in Japanse) where it does two calculation,
(1)Time Elapsed from the Date Employed
(2)Age Calculation(when you press the button, an alert message with the age shows up)
//////////////////////////////////////////////////////////////////
/*
* A sample program to calculate the number of years between two dates(a specified date and today’s date)
* Copyright (c) 2016 Cybozu
*
* Licensed under the MIT License
*/
jQuery.noConflict();
(function($) {
“use strict”;
// Resetting locale
moment.locale(‘ja’);
// Date calculation up to today
function getYearMonth(dtDate) {
var dtToday = moment();
var dtFrom = moment(dtDate);
var years = 0;
var months = 0;
//Calculate if the input date is a past date
if (!dtToday.isBefore(moment(dtFrom), ‘day’)) {
years = dtToday.diff(moment(dtFrom), ‘years’);
months = dtToday.diff(moment(dtFrom), ‘months’) % 12;
}
return years + "Year(s) " + months + “Month(s)”;
}
kintone.events.on(“app.record.detail.show”, function(event) {
var record = event.record; // A record on the page before saving
// Birth Date
var emBirthDay = kintone.app.record.getSpaceElement(‘BirthDay’);
// Date Employed
var emJoiningDate = kintone.app.record.getFieldElement(‘JoiningDate’);
// Displaying the age from the birth date
if (emBirthDay) {
//jQuery
var emButton = $("<button>", {
id: ‘age_button’,
text: ‘Age calculation’
}).click(function() {
//Age Calculation
var valBirthDay = moment().diff(moment(record[‘BirthDay’][‘value’]), ‘years’);
alert(valBirthDay + " years old");
});
$(emBirthDay).append($(emButton));
}
if (emJoiningDate) {
var valJoiningDate = getYearMonth(record[‘JoiningDate’][‘value’]);
var emLabel = $("<label>");
var emDiv = $("<span>");
$(emJoiningDate).append($(emDiv));
$(emJoiningDate).css({
“width”: (parseInt($(emJoiningDate).innerWidth(), 10) + 50) + ‘px’
});
$(emDiv).append($(emLabel));
$(emLabel).html(’<br>’);
$(emLabel).append(valJoiningDate);
$(emDiv).css({
“color”: ‘blue’
});
}
});
})(jQuery);
//////////////////////////////////////////////////////////////////
Please note that the program is using the two libraries listed in Cybozu CDN(which you must use it the screen below):
(1)Moment.js (https://js.cybozu.com/momentjs/2.13.0/moment-with-locales.min.js)
(2)jQuery (https://js.cybozu.com/jquery/2.2.4/jquery.min.js)
*Community.js is just the script above I named it randomly.
From that, you should get something like this(the red circled part) for the (1)Time Elapsed from the Date Employed.
and (2)Age Calculation
I hope this helps you!