Determining age using Javascript within Adobe Acrobat

I've encountered a challenge trying to populate a field in an interactive PDF form. I am utilizing JavaScript to calculate the current age of a client by subtracting two date fields (DateToday and ClientDOB) already present in the form, and then aiming to have it display in a "ClientAge" field. The DateToday field is set to auto-populate when the form is opened. My goal is for the ClientAge field to populate once the user selects a value for ClientDOB.

My desired calculation is as follows, which seems straightforward:

DateToday - ClientDOB = ClientAge

Below is the code I'm using:

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'm puzzled as to why the ClientAge field isn't populating after selecting ClientDOB. Any insights or assistance would be greatly appreciated. Thank you.

Answer №1

This piece of code was sourced from the internet, although the exact origin is unknown. Despite this uncertainty, I have utilized it in various scenarios and found it to be effective. The concept behind it is that the variance between dates is measured in milliseconds, with a specific date representing the number of seconds elapsed since a predetermined date in the past. By determining the difference in seconds between two dates (such as Date of Birth to the present), one can calculate the corresponding number of years. It should be noted that the date format used here is in British style (dd/mm/yy), so adjustments will need to be made for those operating in American format (mm/dd/yy).

// obtain current date IN THIS NON-AMERICAN FORMAT
var oNow = new Date();
// fetch date from 'Demo.DOB' input field
var oMyDate = util.scand('dd/mm/yy', this.getField('Demo.DOB').value);
// define a second in milliseconds
var nSec = 1000;
// define a minute in milliseconds
var nMin = 60 * nSec;
// define an hour in milliseconds
var nHr = 60 * nMin;
// define a day in milliseconds
var nDay = 24 * nHr;
// calculate today's date as the number of days from the epoch date
var nNowDays = Number(oNow) / nDay;
// round down to whole days
nNowDays = Math.floor(nNowDays);
// calculate the inputted date in days from the epoch date
var nMyDateDays = Number(oMyDate) / nDay;
// round down to whole days
nMyDateDays = Math.floor(nMyDateDays);
// determine the difference in the number of days
var nDiffDays = nNowDays - nMyDateDays;
// adjust the difference to account for counting the starting day as 1
++nDiffDays;
// convert days to years
var nYears = nDiffDays / 365.2525
// round down to whole years
nYears = Math.floor(nYears);
// set the value of the field to the calculated number of years (nYears)
event.value = nYears;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The react-native-video-controls package was used in React Native. When the onBack event was triggered, it successfully navigated back to the initial screen. However, upon returning

https://i.stack.imgur.com/GsS3d.png https://i.stack.imgur.com/HYBOU.png Attached are screenshots showing the issue I am experiencing with my video player. When I click the back button, the screen does not return to its initial state and the flatlist items ...

Angular fails to refresh Ng-class after on-change event is activated

Encountering an unusual issue with Angular. While I can trigger the desired action with ng-click, I must utilize onchange for other specific requirements. Although my code works smoothly for other buttons that execute the same action, the upload button fa ...

Vue.js: V-Model input fails to update with JS virtual keyboard

I have integrated JSkeyboard into my webpage and am using v-model to dynamically update the text based on user input. The issue I am facing is that when using a physical keyboard, everything works as expected. However, when using the on-screen JS keyboard, ...

A streamlined method for transforming documents into PDF files

I have been experimenting with different methods to convert document files such as doc, docx, ppt, and pptx to pdf. Up until now, I have tested out docsplit and oowriter, but both of them took more than 10 seconds to complete the conversion for a pptx file ...

How to display a three.js scene in the center of a container

I'm having trouble getting my three.js scene to display above some cards and right below my navigation bar. Currently, the scene is rendering under the cards and is not centered. Despite looking at other forum posts for help, I can't seem to figu ...

Error message "Data required to finish this task is not accessible" when using Ajax Asynchronous in Internet Explorer

My Ajax model in Javascript is 100% valid, with a few inputs such as the method (Get or Post), page to communicate with, string to send, and element on my own page to manipulate upon response. The issue arises when I set the request to Asynchronous; IE thr ...

A guide on crafting a test scenario for an AngularJS controller using the Jasmine framework

I recently created an angular module called userModule.js 'use strict'; angular.module('users', ['ngRoute','angular-growl','textAngular','ngMaterial','ngMessages','ngImgCrop', ...

What is the process for choosing with multiple attribute selectors?

Is there a way to disable multiple fields in a checkbox survey simultaneously? I attempted the following code, but it only works with one class. Is it possible to select by multiple classes within the same div? function surveyInit(){ $("div[class*=&apos ...

Execute asynchronous code in a Next.js component without relying on the UseEffect hook

Within my nextjs application, there is a StrapiImage component that takes in an image object from the strapi backend api as a prop. This component sets the width, height, URL, and any additional props for the image. Essentially, it serves as a shortcut for ...

Changing the size of shapes in cytoscape.js when using the dagre layout

My goal is to resize the shapes of the nodes based on the size of the node text. I tried following the steps outlined in https://github.com/cytoscape/cytoscape.js/issues/649, but encountered some issues: The shapes consistently end up with equal height a ...

Retrieve a PDF file from an HTTP response using Axios

I'm currently working on a project that involves integrating Vue with a Laravel API as the back-end. One of the requirements is to be able to download PDF files from the server when a certain link is clicked. I am using axios for making GET requests, ...

Toggle the visibility of a div by clicking on another div in a

I have created a unique design where a div features a background image of a face, along with a paragraph, two buttons, and an input box inside it. While I know this question has been asked before, my scenario is slightly different. I want the div with the ...

In order to access the localStorage from a different component, a page refresh is required as it is

UPDATE: Just to clarify, this question is NOT duplicate of how to retrieve the value from localstorage. My scenario is unique and the issue lies with Angular itself rather than localStorage. I am currently developing an Angular7 application. In one of my ...

Opting to monitor element visibility over relying solely on page load with Neustar WPM

I need help with writing a Neustar WPM script to time the button click until an overlay appears. Here is what my current script looks like: var webDriver = test.openBrowser(); var selenium = webDriver.getSelenium(); webDriver.get('https://www. ...

Having trouble getting jQuery scrollTop to function with an "else if" statement?

Looking to dynamically add or remove classes from a div based on scrolling position using jQuery. Defined two variables: var mainHeight = $('#main-image').height()/1.10; var footHeight = $('body').height() - $('footer').heig ...

Guide on updating data source in Kendo listview

In order to bind remote data to a ListView based on the date, I have implemented the following method. function getListData(date) { $.ajax({ type: "POST", url: wcfurl + "getList", data: '{"date":"' + date + '"} ...

Ways to alter the color of automatically typing text in HTML

Hello everyone, can you assist me in changing the color of text? I am looking for ways to make text stand out using special colors. Take for example: "discuss about HTML" In HTML, how can you change the color of text without using the <colo ...

tips on adjusting margins or paddings in option select

How can I use the margin or padding options for my select dropdown? I tried using &nbsp; <select class="select"> <option value="100"></option> <option value="250">$593</option> ...

Can you provide an example of JSON for a MultiStep Form that includes a variety of control types for viewing

Could someone please provide me with JSON examples of multi-step forms that include various types of controls such as radio buttons, checkboxes, dropdown menus, and calendars? I am in need of JSON for both the view and edit forms. Your help would be grea ...

Persist the configuration settings of Data tables (such as ColReorder and ColVis plug-ins) by saving and loading them from a database

Does anyone know if it's possible to save and load the states of Data tables (such as ColReorder, ColVis plug-ins) to/from a database? I've tried implementing this, but I'm still facing difficulties. Below is my code for loading the states f ...