Calculate number of days between two dates

Issues related to forms.
Post Reply
garyd
 
 
Posts: 8
Joined: Tue Jul 19, 2022 9:30 am

Calculate number of days between two dates

Post by garyd »

Hi, I'm trying to get the number of days between two editboxes (type=date). I'm using the following calculation in the conditions section to get the difference in millisecs before I do the final calculation to convert to days but not having any luck. I think my coding ignorance is showing!!
=GetTime([Editbox43])-GetTime([Editbox44])
Any help would be greatly appreciated
User avatar
Pablo
 
Posts: 21710
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Calculate number of days between two dates

Post by Pablo »

I cannot help you with programming related questions because for me that may also take a lot of time to figure this out.
But, you can use all standard JavaScript functionality.

Tthis may be helpful:
https://www.w3schools.com/jsref/jsref_obj_date.asp
wwonderfull
 
 
Posts: 1276
Joined: Fri Aug 21, 2020 8:27 am

Re: Calculate number of days between two dates

Post by wwonderfull »

garyd
 
 
Posts: 8
Joined: Tue Jul 19, 2022 9:30 am

Re: Calculate number of days between two dates

Post by garyd »

I'm not asking for progamming code. However, I would like to know why an Editbox condition with the calculation getTime[Editbox1] (which I understand to be the correct command) does not put the time in millisecs since 1st Jan 1970 into the target Editbox, even though Editbox1 has date set as type and the target Editbox has number set as type?
Pablo wrote: Thu Oct 27, 2022 10:21 am I cannot help you with programming related questions because for me that may also take a lot of time to figure this out.
But, you can use all standard JavaScript functionality.

Tthis may be helpful:
https://www.w3schools.com/jsref/jsref_obj_date.asp
User avatar
Pablo
 
Posts: 21710
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Calculate number of days between two dates

Post by Pablo »

If you need further assistance then please created a DEMO project, so we can see if there is an error.
For further details about how to share a project file, please see this FAQ:
https://www.wysiwygwebbuilder.com/forum ... 10&t=82134

You can also check the error console of the browser. That will probably give you a clue what is wrong.

Note that the type of the editbox affects how the browser renders the input field. This can be different for each browser. This is unrelated to WWB.
garyd
 
 
Posts: 8
Joined: Tue Jul 19, 2022 9:30 am

Re: Calculate number of days between two dates

Post by garyd »

Hi, please find below the requested link to the DEMO page along with the page HTML. I have tried using Firefox and Edge to run the code in:



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 17 - https://www.wysiwygwebbuilder.com">
<link href="EzSocial.css" rel="stylesheet">
<link href="page2.css" rel="stylesheet">
<script src="jquery-3.6.0.min.js"></script>
<script>
function submitForm1()
{
var regexp;
var Editbox1 = document.getElementById('Editbox1');
if (!(Editbox1.disabled || Editbox1.style.display === 'none' || Editbox1.style.visibility === 'hidden'))
{
regexp = /(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}/;
if (Editbox1.value.length != 0 && !regexp.test(Editbox1.value))
{
alert("The specified value is invalid.");
Editbox1.focus();
return false;
}
}
return true;
}
</script>
<script>
$(document).ready(function()
{
$("#Editbox1").change(function()
{
$('#Editbox2').val(getTime($('#Editbox1').val()));
});
$("#Editbox1").trigger('change');
});
</script>
</head>
<body>
<div id="wb_Form1" style="position:absolute;left:27px;top:36px;width:278px;height:266px;z-index:2;">
<form name="Form1" method="post" action="mailto:yourname@yourdomain.com" enctype="multipart/form-data" id="Form1" onsubmit="return submitForm1()">
<input type="date" id="Editbox1" style="position:absolute;left:23px;top:26px;width:157px;height:23px;z-index:0;" name="box1" value="" spellcheck="false">
<input type="number" id="Editbox2" style="position:absolute;left:23px;top:112px;width:215px;height:33px;z-index:1;" name="box2" value="" spellcheck="false">
</form>
</div>
</body>
</html>
Pablo wrote: Thu Oct 27, 2022 8:01 pm If you need further assistance then please created a DEMO project, so we can see if there is an error.
For further details about how to share a project file, please see this FAQ:
https://www.wysiwygwebbuilder.com/forum ... 10&t=82134

You can also check the error console of the browser. That will probably give you a clue what is wrong.

Note that the type of the editbox affects how the browser renders the input field. This can be different for each browser. This is unrelated to WWB.
Last edited by garyd on Wed Nov 16, 2022 1:13 pm, edited 1 time in total.
User avatar
BaconFries
 
 
Posts: 5364
Joined: Thu Aug 16, 2007 7:32 pm

Re: Calculate number of days between two dates

Post by BaconFries »

When it is asked for a "Demo" it is a copy of your project (.wbs) file. This is because just looking at the source (html) doesn't let us see what settings you have used. Can you please upload a copy of the .wbs and provide a link so to download from.
User avatar
Pablo
 
Posts: 21710
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Calculate number of days between two dates

Post by Pablo »

There is no 'getTime' function in JavaScript, see also the error in the console of the browser.

Note that this error is unrelated to WWB.
User avatar
crispy68
 
 
Posts: 2749
Joined: Thu Oct 23, 2014 12:43 am
Location: Acworth, GA
Contact:

Re: Calculate number of days between two dates

Post by crispy68 »

I have not implemented this in WB in the workspace but it should work if you add the code to the correct spots.

I changed your function in the head section to this:

Code: Select all

<script>
function GetDays(){
  var date1 = new Date(document.getElementById("Editbox1").value);
  var date2 = new Date(document.getElementById("Editbox2").value);
  return parseInt(Math.abs((date1 - date2) / (24 * 3600 * 1000)));
}
function cal(){
  if(document.getElementById("Editbox1")){document.getElementById("numdays").value = GetDays();}
}
</script>
<style>
#numday{position:absolute;left:0;top:300px;width:600px;font-family:arial;font-size:16px;color:000;}
</style>
I added style code above to style the output box I inserted in your code. However, if you add a box within WB you should be able to style it within WB and not need to add this manually.

In the body section, I changed Editbox2 to type=date. You have 2 date boxes. I added the label "Number of Days" div section below. I also added "onchange" code to each editbox. See below:

Code: Select all

<div id="wb_Form1" style="position:absolute;left:27px;top:36px;width:278px;height:266px;z-index:2;">
<form name="Form1" method="post" action="mailto:yourname@yourdomain.com" enctype="multipart/form-data" id="Form1" onsubmit="return submitForm1()">
<input type="date" id="Editbox1" style="position:absolute;left:23px;top:26px;width:157px;height:23px;z-index:0;" name="box1" value="" spellcheck="false" onchange="cal()">
<input type="date" id="Editbox2" style="position:absolute;left:23px;top:112px;width:215px;height:33px;z-index:1;" name="box2" value="" spellcheck="false" onchange="cal()">

<div id="numday"><label>Number of days:</label><input type="text" id="numdays" name="numday"/></div>
</form>
</div>
It may still need some tweaking but this should get you in the right direction. :D
User avatar
crispy68
 
 
Posts: 2749
Joined: Thu Oct 23, 2014 12:43 am
Location: Acworth, GA
Contact:

Re: Calculate number of days between two dates

Post by crispy68 »

Is this what you are after?

https://webbuildertemplates.com/demo/date/

If so, I can provide a copy of a demo project. I've tweaked the code as well.
garyd
 
 
Posts: 8
Joined: Tue Jul 19, 2022 9:30 am

Re: Calculate number of days between two dates

Post by garyd »

Hi, that's exactly what I'm after!!
Sorry for the delay in replying but I've been away from the computer for a while.
crispy68 wrote: Fri Oct 28, 2022 5:39 pm Is this what you are after?

https://webbuildertemplates.com/demo/date/

If so, I can provide a copy of a demo project. I've tweaked the code as well.
garyd
 
 
Posts: 8
Joined: Tue Jul 19, 2022 9:30 am

Re: Calculate number of days between two dates

Post by garyd »

I've just copied across the code and got it working. Thanks for your help!!
crispy68 wrote: Fri Oct 28, 2022 1:50 pm I have not implemented this in WB in the workspace but it should work if you add the code to the correct spots.

I changed your function in the head section to this:

Code: Select all

<script>
function GetDays(){
  var date1 = new Date(document.getElementById("Editbox1").value);
  var date2 = new Date(document.getElementById("Editbox2").value);
  return parseInt(Math.abs((date1 - date2) / (24 * 3600 * 1000)));
}
function cal(){
  if(document.getElementById("Editbox1")){document.getElementById("numdays").value = GetDays();}
}
</script>
<style>
#numday{position:absolute;left:0;top:300px;width:600px;font-family:arial;font-size:16px;color:000;}
</style>
I added style code above to style the output box I inserted in your code. However, if you add a box within WB you should be able to style it within WB and not need to add this manually.

In the body section, I changed Editbox2 to type=date. You have 2 date boxes. I added the label "Number of Days" div section below. I also added "onchange" code to each editbox. See below:

Code: Select all

<div id="wb_Form1" style="position:absolute;left:27px;top:36px;width:278px;height:266px;z-index:2;">
<form name="Form1" method="post" action="mailto:yourname@yourdomain.com" enctype="multipart/form-data" id="Form1" onsubmit="return submitForm1()">
<input type="date" id="Editbox1" style="position:absolute;left:23px;top:26px;width:157px;height:23px;z-index:0;" name="box1" value="" spellcheck="false" onchange="cal()">
<input type="date" id="Editbox2" style="position:absolute;left:23px;top:112px;width:215px;height:33px;z-index:1;" name="box2" value="" spellcheck="false" onchange="cal()">

<div id="numday"><label>Number of days:</label><input type="text" id="numdays" name="numday"/></div>
</form>
</div>
It may still need some tweaking but this should get you in the right direction. :D
User avatar
crispy68
 
 
Posts: 2749
Joined: Thu Oct 23, 2014 12:43 am
Location: Acworth, GA
Contact:

Re: Calculate number of days between two dates

Post by crispy68 »

@garyd,

I slightly updated the code in my demo versus what I posted previously. You can download the project file here to get the latest.
dahdah
 
 
Posts: 28
Joined: Wed Oct 18, 2017 7:58 pm

Re: Calculate number of days between two dates

Post by dahdah »

bonjour monsieur merci pour le code c'est génial
est-ce que vous pouvez m'aider pour que la première date (Editbox1) soit fixée par exemple 31/12/2023 le client ne peut pas la changer
mais il peut entrer la date de la fin à la deuxième date
et le calcul se fait

Image

Image
dahdah
 
 
Posts: 28
Joined: Wed Oct 18, 2017 7:58 pm

Re: Calculate number of days between two dates

Post by dahdah »

bonjour monsieur merci pour le code c’est génial
est-ce que vous pouvez m’aider pour que la deuxième date (Editbox2) soit fixée par exemple 31/12/2023 le client ne peut pas la changer
mais il peut entrer la date de la fin à la première date
et le calcul se fait

Image

Image
Post Reply