• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

calculating in weeks and days

Community Beginner ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

Hello, I need your help please, 

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

Views

671

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , 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
...

Votes

Translate

Translate
Community Beginner ,
Nov 24, 2019 Nov 24, 2019

Copy link to clipboard

Copied

I'm using adobe acrobat pro 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

thank you so much

You are great

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

thanks again try67

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
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"; } }

Votes

Translate

Translate

Report

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