Skip to main content
waelk38411626
Known Participant
November 24, 2019
Answered

calculating in weeks and days

  • November 24, 2019
  • 3 replies
  • 1010 views

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

This topic has been closed for replies.
Correct answer try67

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.

3 replies

waelk38411626
Known Participant
November 25, 2019

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"; } }

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 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 = 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.

Inspiring
November 25, 2019

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.

waelk38411626
Known Participant
November 24, 2019

I'm using adobe acrobat pro