Skip to main content
waelk38411626
Known Participant
November 28, 2019
Question

calculating in weeks

  • November 28, 2019
  • 4 replies
  • 723 views

Help please 

if I need to calculate the difference between two dates in weeks and days.

how do I do please ?

  

I have 3 feilds 

the first "DG", the second "date", the third "age

I can make the diffrenece in weeks or days but not both

please help me

event.value = 0;var strStart = this.getField("dob").value;var strEnd = this.getField("date").value;if(strStart.length & strEnd.length) {var Start1 = util.scand("dd/mm/yyyy",strStart);var End1 = util.scand("dd/mm/yyyy",strEnd);var diff = End1.getTime() - Start1.getTime();var oneDay = 7 * 24 * 60 * 60 * 1000;var days = Math.floor(diff/oneDay);event.value = days;}

This topic has been closed for replies.

4 replies

Legend
November 29, 2019

Thank you for clarifying. This code (a model, not a solution) should give you the ideas you need to get the answer you  want:

// Let's suppose the calculation is for 23 days, in total days

total_days=23
// Calculate exact_weeks, the exact number of weeks (including decimal places)
exact_weeks = total_days / 7 ;

// Round this down to a whole number of weeks in whole_weeks
whole_weeks = Math.floor(exact_weeks) ;

// To calculate the days over, first work out how many days in these whole weeks (whole_weeks_as_days)
whole_weeks_as_days = whole_weeks * 7 ;

// Now work out days_over, the extra days in the current week
days_over = total_days - whole_weeks_as_days ;

Bernd Alheit
Community Expert
Community Expert
November 29, 2019
waelk38411626
Known Participant
November 29, 2019

It didn't work

waelk38411626
Known Participant
November 29, 2019

what i need is to calculate the array between tw dates in weeks and days 

so that i can know the gestational age the day thar my patient has made her examination 

Legend
November 29, 2019

Coders can't work with "one week or two". Code cannot be written until the rules are precisely known, even if that is stricter than people need.  (Also, such a calculation has very serious legal implications, and should not be left up to coders to pick something at random!!) 

waelk38411626
Known Participant
November 29, 2019

It is to calculate gestationnal age 

supposing the conception date is 02/11/2019 so the pregnancy age is to 4 weeks 

Legend
November 28, 2019

You have to decide exactly how you want to calculate "difference in weeks", there are different ways. How would you do it on paper? For example, Monday 1 to Monday 8. One week or two? Friday 5 to Thursday 11. One week or two? 10 am on Monday 1 to 9 am on Monday 8. One week or two? 

waelk38411626
Known Participant
November 29, 2019

nombre of weeks and days, this calculating isfor pregnancy