Jump to content
  • 0

Calculating Period From Date


Parma2015

Question

Hi!

I have a submission Form in which I want to calulate the period YYMM from the date selected in a calendar Popup.

 

I have tried different variations of scripts I have found here, but haven'n got it right so far.

Maybe I am mixing up the formats date, string og number?

I usually define period as a number, and when I fetch year and month the period is calculated by year *100 + month.

If year and month are string values, it should be sometheing like left(year,2) &mnd (Visual Basic).

Does anyone have a solution to this?

I tried this, hoping that my v_year and v_month are number values

>SCRIPT>

function calculate()

{

v-date = document.getElementByID("InsertRecordDate").Value;

v_year = v.date.getFullYear(); 

v_month = v_date.getMonth();

v_period = v_year * 100 + v_month 

document.getElementById("InsertRecordPeriod").Value = Math.round(v_period);

 

document.getElementBy ID("caspioform")=.onsubmit.calculate;

</SCRIPT>

 

Am I close? :-)

 

Jan H

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Hi Jan,

 

How are you? How's the weather in your city?

I've corrected your code a bit:

<SCRIPT LANGUAGE="JavaScript">

function setDate()
{
     var stringDate = document.getElementById("InsertRecordDate").value;
     var v_Date = new Date(Date.parse(stringDate));
     v_year = v_Date.getFullYear();
     v_month = v_Date.getMonth();
     v_period = v_year * 100 + v_month + 1;
     document.getElementById("InsertRecordPeriod").value=Math.round(v_period);
}   

document.getElementById("caspioform").onsubmit=setDate;
</SCRIPT>

getMonth returns the number of a month, started from 0, so I added "+1" to the correspond line.

 

 

I'll be grateful, if you tell me if the code works.

Have a nice day!

Link to comment
Share on other sites

  • 0

Hi!

Thanks!

Your code worked as for trying to write to the field period, but I am getting a message "NaN" which means its not a number? In my table I had datatype = Integer, and when I changed it to text, the DataPage stored the value "NaN" as text.

I tried also to use datatype integer, and the code parseInt in before the v_month and v_year, but I still couldn't get it to work.

I have the feeling that this is very close :-)

By the way, the weather here inn Norway is clear sky, but not more the 10 degrees centigrade :-/

Jan H

Link to comment
Share on other sites

  • 0

Hi Jan,

 

I've found a mistake in my code. I hope, now everything is fixed:

<SCRIPT LANGUAGE="JavaScript">

function setDate()
{
     var stringDate = document.getElementById("InsertRecordDate").value;
     var v_Date = new Date(Date.parse(stringDate));
     var v_year = parseInt(v_Date.getFullYear());
     var v_month = parseInt(v_Date.getMonth());
     var v_period = v_year * 100 + v_month + 1;
     v_period = parseInt(v_period);
     document.getElementById("InsertRecordPeriod").value=v_period;
}   

document.getElementById("caspioform").onsubmit=setDate;
</SCRIPT>

I thought, it's colder in Norway on Halloween :) And if I understand correctly, the 10 degrees centigrade is rather cold and should be even warmer?

 

Have a nice day!

Link to comment
Share on other sites

  • 0

Hi Jan,

 

Add alerts to see, where the problems start :)

<SCRIPT LANGUAGE="JavaScript">

function setDate()
{
alert("Start");
     var stringDate = document.getElementById("InsertRecordDate").value;
alert(stringDate);
     var v_Date = new Date(Date.parse(stringDate));
alert(v_Date);
     var v_year = parseInt(v_Date.getFullYear());
alert(v_year);
     var v_month = parseInt(v_Date.getMonth()) + 1;
alert(v_month);
     var v_period = v_year * 100 + v_month;
alert(v_period);
     v_period = parseInt(v_period);
alert(v_period);
     document.getElementById("InsertRecordPeriod").value=v_period;
}   

document.getElementById("caspioform").onsubmit=setDate;
</SCRIPT>

In which alert the first "NaN" is displayed?

Link to comment
Share on other sites

  • 0

Hi again Jan,

 

I have tried with the Norwegian localization, and I get "NaN" as well. That's surprise for me!

 

I have rewritten the code as follows:

<SCRIPT LANGUAGE="JavaScript">
function setDate()
{
     var stringDate = document.getElementById("InsertRecordDate").value;
     var strDays = stringDate.slice(0, 2);
     var strMonths = stringDate.slice(3, 5);
     var strYears = stringDate.slice(6);
     var strDate = strMonths + "/" + strDays + "/" + strYears;
     var v_Date = new Date(Date.parse(strDate));
     var v_year = parseInt(v_Date.getFullYear());
     var v_month = parseInt(v_Date.getMonth());
     var v_period = v_year * 100 + v_month + 1;
     v_period = parseInt(v_period);
     document.getElementById("InsertRecordPeriod").value=v_period;
}   
document.getElementById("caspioform").onsubmit=setDate;
</SCRIPT>

And then I realized, that the code should be shorter, like follows:

<SCRIPT LANGUAGE="JavaScript">
function setDate()
{
     var stringDate = document.getElementById("InsertRecordDate").value;
     var v_period = parseInt(stringDate.slice(6)) * 100 + parseInt(stringDate.slice(3, 5));
     document.getElementById("InsertRecordPeriod").value=parseInt(v_period);
}   
document.getElementById("caspioform").onsubmit=setDate;
</SCRIPT>

I'll be grateful, if you tell me if the code works. I hope they do!

Have a nice day!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...