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
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
...
Copy link to clipboard
Copied
I'm using adobe acrobat pro
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.
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.
Copy link to clipboard
Copied
thank you so much
You are great
Copy link to clipboard
Copied
thanks again try67
Copy link to clipboard
Copied
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"; } }