calculate age javascript in adobe acrobat - javascript

I am having some difficulties getting a field to populate in an interactive PDF form. I am using a javascript to calculate the current age of client from 2 date fields (DateToday and ClientDOB) already in the form and I need it to populate a "ClientAge" field. The DateToday field automatically populates when the form is opened. I would like for the ClientAge field to populate after the user selects the ClientDOB.
This is what I am trying to have it do. Should be simple I would think.
DateToday - ClientDOB = ClientAge
Here is my code:
var DateToday_ = Date2Num(DateToday.formattedValue, "MM/DD/YYYY")
var ClientDOB_ = Date2Num(ClientDOB.formattedValue, "MM/DD/YYYY")
var diff = DateToday_ - ClientDOB_
ClientAge.value = Floor(diff / 365.25)
I am not sure why the ClientAge field will not populate once the ClientDOB has been selected. Any replies would be helpful. Thanks.

This was taken from somewhere off the 'net. Can' remember where. However I have used this in a number of forms and it works fine. The idea is that the difference between dates is in milliseconds, and a given date is the number of seconds from a fixed date in the past. Once you have the difference in seconds between the dates (in this case DOB to the present) you can calculate how many years that is. Note that my format is in British date format (dd/mm/yy). If you operate in American format (mm/dd/yy) you must make the appropriate changes.
// get current date THIS NON AMERCAN DATE FORMAT
var oNow = new Date();
// get date from 'Demo.DOB' field
var oMyDate = util.scand('dd/mm/yy', this.getField('Demo.DOB').value);
// define second in milliseconds
var nSec = 1000;
// define minute in milliseconds
var nMin = 60 * nSec;
// define hour in milliseconds
var nHr = 60 * nMin;
// define day in milliseconds
var nDay = 24 * nHr;
// compute today as number of days from epoch date
var nNowDays = Number(oNow) / nDay;
// truncate to whole days
nNowDays = Math.floor(nNowDays);
// compute inputted date days from epoch data
var nMyDateDays = Number(oMyDate) / nDay;
// truncate to whole days
nMyDateDays = Math.floor(nMyDateDays);
// compute difference in the number of days
var nDiffDays = nNowDays - nMyDateDays;
// adjust difference for counting starting day as 1
++nDiffDays;
// convert days to years
var nYears = nDiffDays / 365.2525
// truncate to whole years
nYears = Math.floor(nYears);
// set field value number of years (nYears)
event.value = nYears;

Related

How to get future date based on timestamp ratio

I am trying to have a virtual calendar for a game.
I have events that will last a certain time in reality (1 week, 1 month).
In the game those events should always be equal to 1 year.
To make things simple my goal was to use date-fns and work with timestamps to keep a ratio.
Lets say I have an event running for 1 week (real life)
In game that would be 1 year.
If I try to get the in-game date when I'm at event start + 3 days (almost half of the event passed). Theoretically I should be close to 6 months in the virtual calendar however while testing its gives me an answer years later.
import {
addDays,
addYears,
getTime
} from "date-fns";
// Initiate Dates
const now = new Date()
const nextWeek = addDays(now, 7);
const nextYear = addYears(now, 1);
// Initiate Timestamp convertions
const currentTimestamp = getTime(now)
const tmrTimestamp = getTime(addDays(now, 3))
const nextWeekTimestamp = getTime(nextWeek)
// Calculate differences
const differenceReal = nextWeekTimestamp - currentTimestamp
const differenceVirtual = getTime(nextYear) - currentTimestamp
console.log(`difference_real : ${differenceReal}`)
console.log(`difference_virtual : ${differenceVirtual}`)
// Calculate the ratio
const ratio = differenceReal / differenceVirtual
// Log information
console.log(`ratio: ${ratio}`)
console.log(`ts_now ${getTime(now)}`)
console.log(`ts_tmr ${getTime(tmrTimestamp)}`)
//Calculate equivalence of day+1 on a year
const nextDayRatioed = tmrTimestamp / ratio
console.log(`ts_ratioed: ${Math.round(nextDayRatioed)}`)
console.log(`ts_next_year: ${getTime(nextYear)}`)
console.log(`next_year: ${nextYear.toLocaleString()}`)
console.log(`tmr_relative: ${new Date(Math.round(nextDayRatioed)).toLocaleString()}`)
Output:
How could I get the tmr_relative to be the correct value which would be more or less January of 2022
You must keep as constant
the start time of your game as the point of origin.
the ratio that you want for time. In your case 1 week is 1 year for your game.
Check the following way to achieve that using only date.
const ratio = 365/7; //This is the virtual ration that you want
const nowReal = new Date() //This would always be the basis to compare
//Use a fixed date one week later to test how it would behave
const nextWeekReal = new Date();
nextWeekReal.setDate(nextWeekReal.getDate() + 7);
//Use a fixed date 2 week later to test how it would behave
const doubleNextWeekReal = new Date();
doubleNextWeekReal.setDate(doubleNextWeekReal.getDate() + 14);
//Check the next week virtual date
console.log(virtualDate(nowReal, datediff(nowReal, nextWeekReal), ratio));
//Check after 2 weeks the virtual date
console.log(virtualDate(nowReal, datediff(nowReal, doubleNextWeekReal), ratio));
function datediff(first: any, second: any) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24));
}
function virtualDate(basis: Date, diff: number, ration: number){
const virtualDate = new Date();
virtualDate.setDate(basis.getDate() + diff * ratio);
return virtualDate;
}
Result considering that you start the game now on 24/7/21.
After 1 week have passed in real time it will print you 1 year later from the point of origin
After 2 weeks have passed in real time it will print you 2 years later from the point of origin
Lets say I have an event running for 1 week (real life) In game that
would be 1 year. If I try to get the in-game date when I'm at event
start + 3 days (almost half of the event passed). Theoretically I
should be close to 6 months
//Use a fixed date half a week later to test how it would behave
const halfWeekReal = new Date();
halfWeekReal.setDate(halfWeekReal.getDate() + 3);
console.log("Half Week have passed in real time " + halfWeekReal);
//Check after half week the virtual date
console.log("Virtual date will be " + virtualDate(nowReal,
datediff(nowReal, halfWeekReal), ratio));
This will print
It is about 5 months, which is the correct behavior you have described.
Here's a solution using plain old vanilla JS.
let refInGameDate = new Date()
document.querySelector('#days').addEventListener('change', e => {
let days = +e.target.value
let years = (Math.floor(days / 7) + (days % 7 / 7)).toFixed(2)
document.querySelector('#ingame').innerText = years
let rd = refInGameDate.getTime() + (years * 365 * 24 * 60 * 60 * 1000)
document.querySelector('#indate').innerText = new Date(rd).toString();
})
<input type='number' id='days' /> real-time days
<hr>
<div>in-game translation: <span id='ingame'></span> years</div>
<div>date representation: <span id='indate'></span></div>

Date Math for Google Apps Script for User Submitted Dates

I created a Google Form to collect user input, including the expiration date of a contract.
I need to create a reminder date (6 months before the expiration date) in a new column of the gsheets that is linked to the form. Using the Event Object namedValues, I extracted the expiration date from gsheet. I converted the date to milliseconds and subtracted the number of milliseconds equal to 6 months (or thereabouts). However, the output that got sent back to the googlesheet is an undefined number.
I must be misunderstanding something and was hoping someone more skilled in this can help me out. Is the data type wrong? Thanks for any illumination you can provide.
function onFormSubmit(e) {
var responses = e.namedValues;
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var expireDate = responses['Expiration Date'][0].trim();
var expireDate_ms = expireDate * 1000; // converting to milliseconds
var noticeDate = expireDate_ms - (183 * MILLIS_PER_DAY);
// Create a new column to store the date to send out notice of expiration or renewal
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var column = e.values.length + 1;
sheet.getRange(row, column).setValue(noticeDate);
}
I’d recommend you to use a formula instead:
={"Reminder"; ARRAYFORMULA(IF(NOT(ISBLANK(B2:B)); EDATE(B2:B; -6); ""))}
Add this to the header of an empty column, it will generate all data in that column for you. Change B for the column you have the expiration date on.
If you really need to use Google Apps Script you can, but JavaScript is notorious for having bad date support (at least without an external library). To do it, you’ll have to manually parse the string, modify the date and format it back to the date number:
const dateParts = e.namedValues['Expiration Date'][0].trim().split('/')
const date = new Date(
Number(dateParts[2]), // Year
Number(dateParts[1]) - 1 - 6, // Month. -1 because January is 0 and -6 for the 6 months before
Number(dateParts[0]) // Day
)
const numericDate = Math.floor((date.getTime() - new Date(1900, 0, 1).getTime()) / (1000 * 60 * 60 * 24))
This example only works if the format used in the sheet is DD/MM/YYYY.
The numeral value of date is the number of days since the first day of 1900 (link to documentation). So we need to subtract it and change it from milliseconds to days. Math.floor ensures that it’s not decimal.
You can set numericDate to the cell but make sure the numeric format is Date.
References
EDATE - Docs Editors Help
ARRAYFORMULA - Docs Editors Help
Date - JavaScript (MDN)
DATE - Docs Editor Help
Marti's answer was helpful but the math didn't quite work out because the suggested solution was to minus 6 from the month retrieved. But the reminder date is supposed to be 6 months from the date (taking into account the year and the date), so it doesn't quite work.
The solution I worked out is the following:
function onFormSubmit(e) {
const expireDateParts = e.namedValues['Expiration Date'][0].trim();
if (expireDateParts != ""){
expireDateParts.split('/');
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
const expireDate = new Date(
Number(expireDateParts[2]), // Year
Number(expireDateParts[0]) - 1, // Month. -1 because January is '0'
Number(expireDateParts[1]) // Day
);
const reminderNumericDate = Math.floor((expireDate.getTime() - 183 * MILLIS_PER_DAY));
var reminderDate = formatDate(reminderNumericDate);
// Create a new column to store the date to send out notice of expiration or renewal
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var column = 14 // hard-coded to Column 0;
sheet.getRange(row, column).setValue(reminderDate);
// Set up Schedule Send Mail
createScheduleSendTrigger(reminderNumericDate);
var status = 'Scheduled';
sheet.getRange(row, column+1).setValue(status);
}
}

Convert string to date and alter that date

Good day, I am generating 3 dates from a string, I hope the output was:
billing date: 2020/01/11
cutoff start: 2019/11/11
cuttof end: 2019/12/10
but I get the following:
billing date: 2020/11/10
cutoff start: 2019/11/10
cuttof end: 2019/12/10
I would like to know how javascript works with variables or what is the problem since everything is altered
var month = "Jan-20"
var coverage_month_obj = moment(month, 'MMM-YY').toDate();
var billing_date = new Date(coverage_month_obj.setDate(coverage_month_obj.getDate() + 10))
var cutoff_end = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
cutoff_end = new Date(billing_date.setDate(billing_date.getDate() - 1))
var cutoff_start = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
I would like to know how javascript works with variables or what is the problem since everything is altered
Put simply, calling setXXX on a javascript date variable updates that variable in place. ie, it is what we would call "mutable". You might have assumed dates were immutable and did not change in place.
To answer on a better way to achieve your goal, I'd suggest using the other functionality of momentjs to calculate your 3 dates from the given input string.
var month = "Jan-20"
var coverage_month = moment(month, 'MMM-YY');
//Jan-20 I need to convert it into date format and that the day is 11 (2020/01/11) cutoff start, are two months less from that date (2020/11/11) and cutoff end is one month less from Jan-20, but ends on day 10 (2020/12/10)
var billing_date = coverage_month.clone().add(10, 'days');
var cutoff_start = billing_date.clone().subtract(2, 'months');
var cutoff_end = billing_date.clone().subtract(1,'months').subtract(1,'day')
console.log("billing_date",billing_date);
console.log('cutoff_start',cutoff_start);
console.log('cutoff_end',cutoff_end);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Creating a version number based on last modified date

My function looks like this:
$(document).ready(function(){
var d = new Date(document.lastModified);
// document.getElementById("lastup").innerHTML = d;
$("#lastup").html(d);
// Create a version number based on the date
var cy = new Date();
var y = cy.getFullYear()-2015; // ex: 2018 - 2015 = 3 third year of program
var mo = d.getMonth()+1; // ex: 2 = Feb
var dy = d.getDate(); // ex: 12 = Date of month
var v = 'v'+y+'.'+mo+'.'+dy; // ex: v3 = third year of program
$("#version2").html(v); // ex: v3.2.12
});
The goal being to create a version number based on the last time the page code was changed. You can see the year part is based on years the program has been in place. But the month and day take there values from 'd' which is the value of "new Date(document.lastModified)" put it all together and you get a version number. But in this case it gives me a new version number for every day, it does not hold the last modified data, it always increments based on the date. Somewhere I'm not seeing the obvious here. Why is it doing this?

using the time as a dynamic variable to compare values

I am working on programming a page in JS that grabs calendar data from an outside source, imports it into a multidimensional array and uses it to display who is currently working along with their photo, phone number, etc.
Right now I have it set up so that the page reloads every 15 minutes. I'd prefer to have this all done dynamically so that when, say, the clock strikes 5pm the page knows to update without having to wait until the 15 minute refresh is triggered.
All of the work times are pulled from the other calendar in 24 hour format (so 5pm is 1700).
Here's how I'm generating the current time to compare with the start/end times in the calendar:
//Get the current date and time
var dateTime = new Date();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var dayOfWeek = dateTime.getDay();
var year = dateTime.getYear() + 1900;
//converting hours and minutes to strings to form the 24h time
var hours = dateTime.getHours().toString();
if (hours.length === 1) {
var hours = '0' + hours
};
var minutes = dateTime.getMinutes().toString();
if (minutes.length === 1) {
var minutes = '0' + minutes
};
var time = hours + minutes;
//convert the 24h time into a number to read from later
var timeNumber = parseInt(time);
I then use if statements to compare the start/end times from the imported schedule with timeNumber to determine who is currently working and push that to an array that is eventually displayed on the page with this code:
//figure out who is currently working and put them in the workingNow array
var workingNow = [];
for (i = 0; i < workingToday.length; i++){
//convert time strings to numbers to compare
var startTime = parseInt(workingToday[i][7]);
var endTime = parseInt(workingToday[i][8]);
//compare start and end times with the current time and add those who are working to the new list
if(startTime < timeNumber && timeNumber < endTime){
workingNow.push(workingToday[i]);
}
};
I guess I have just been trying to figure out how to make this comparison of the data in an array with the current time something that is dynamic. Is this possible or would I need to go about this in a completely different way from the ground up?
You should have a look at momentjs. This is a really good library to handle all sort of time and date manipulation.
http://momentjs.com/

Categories

Resources