# calculating in weeks and days

New Here ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

I need to calculate pregnancy age in weeks and days.

I have a feild called DG (date de début de grossesse)

and another feild called TA (terme actuel)

I want to have the résultat in weeks and days in the feild TA : nombre of weeks followed by "SA"  and nombre of days folowed by "jours"

can you help me please

thanks anyway

TOPICS
Acrobat SDK and JavaScript, Create PDFs, How to, PDF forms

Views

208

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## 1 Correct Answer

Adobe Community Professional , Nov 24, 2019 Nov 24, 2019
Date calculations are not simple, but I wrote for you the code that does it. You can use it as the custom calculation script of the TA field: var dateFormat = "dd/mm/yyyy"; var startDateString = this.getField("DG").valueAsString; event.value = ""; if (startDateString!="") { var startDate = util.scand(dateFormat, startDateString); if (startDate!=null) { var now = new Date(); var dayInMs = 86400000; var diff = Math.round((now.getTime()-startDate.getTime())/dayInMs); var diffInWeeks = M...

Likes

6 Replies 6
New Here ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

I'm using adobe acrobat pro

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

Date calculations are not simple, but I wrote for you the code that does it.

You can use it as the custom calculation script of the TA field:

``````var dateFormat = "dd/mm/yyyy";
var startDateString = this.getField("DG").valueAsString;
event.value = "";
if (startDateString!="") {
var startDate = util.scand(dateFormat, startDateString);
if (startDate!=null) {
var now = new Date();
var dayInMs = 86400000;
var diff = Math.round((now.getTime()-startDate.getTime())/dayInMs);
var diffInWeeks = Math.floor(diff/7);
var diffInDays = diff-(diffInWeeks*7);
event.value = diffInWeeks + " SA " + diffInDays + " jours";
}
}``````

Adjust the time-format in the first line, if needed.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
LEGEND ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

The line

``var now = new Date();``

includes the hours, minutes, seconds, and milliseconds since midnight. While the util.scand function does not include this when only a date string is converted. This could end up providing a wrong answer depending upon the time of day.

I solve this problem by forcing both times to midnight for the day using the setHours() method.

If both times are corrected to midnight, then one has to include the day on conception.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 25, 2019 Nov 25, 2019

Copy link to clipboard

Copied

thank you so much

You are great

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 25, 2019 Nov 25, 2019

Copy link to clipboard

Copied

thanks again try67

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 25, 2019 Nov 25, 2019

Copy link to clipboard

Copied

LATEST

if I need to calculate the TP in another date (any date)

how do I do please

sorry I'm Begginer in Javascript

and thank you anyway

this my script which is not working

I have 3 feilds

the first "DG

the second "terme" the date at which i want to calcule in weeks and days

the third the "TP"

var startDate = new Date(this.getField("DG").value); var endDate = new Date(this.getField("terme").value); var dateFormat = "dd/mm/yyyy"; if (startDateString!="") { var startDate = util.scand(dateFormat, startDateString); if (startDate!=null) { var now = terme(); var dayInMs = 86400000; var diff = Math.round((now.getTime()-startDate.getTime())/dayInMs); var diffInWeeks = Math.floor(diff/7); var diffInDays = diff-(diffInWeeks*7); event.value = diffInWeeks + " SA " + diffInDays + " jours"; } }

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more