Parma2015 Posted October 26, 2015 Report Share Posted October 26, 2015 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 piyaliguha90 1 Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted October 28, 2015 Author Report Share Posted October 28, 2015 Hi! I have managed to manipulate a date from the format 2015/10/28 into 201510, and also to submit the value to my table. What I have not achieved is to get the date from my Calendar Popup (date) and to assign the value to my variable v_date Any experience or ideas anyone? Jan H Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted October 28, 2015 Report Share Posted October 28, 2015 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! Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted October 28, 2015 Author Report Share Posted October 28, 2015 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 Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted October 29, 2015 Report Share Posted October 29, 2015 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! Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted October 30, 2015 Author Report Share Posted October 30, 2015 Hi! The code still returns NaN for some reason. That is when I format the field as number in the table. And the 10 degrees at this time of year is about normal :-) Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted October 30, 2015 Report Share Posted October 30, 2015 Hi Jan, Can I see the URL of the page? You can send it in the message. Wow! I know more about Norway now And Happy Halloween! Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted November 2, 2015 Author Report Share Posted November 2, 2015 Hi! I tried to paste the uRL in here, but for some reason it is not possible.. Any tricks? Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted November 3, 2015 Report Share Posted November 3, 2015 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? Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted November 3, 2015 Report Share Posted November 3, 2015 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! Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted November 4, 2015 Author Report Share Posted November 4, 2015 Hi! This last code worked perfectly! Thank Youuuu! Now I learnt also how to display values as I go with the "alert". You see, I am used to VBA with the development tools available, but I have not yet come to this point in Javascript. This was really helpful. Regards Jan H Arntzen Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted November 4, 2015 Report Share Posted November 4, 2015 Hi Jan, I'm glad that my scripts helped! And I learned several new facts about Norway! Thank you! Quote Link to comment Share on other sites More sharing options...
0 Parma2015 Posted November 5, 2015 Author Report Share Posted November 5, 2015 Thanks again. Where in the world are you located? Regards Jan H Quote Link to comment Share on other sites More sharing options...
0 Xiang Posted November 5, 2015 Report Share Posted November 5, 2015 You're very welcome! Now I'm in Ukraine, we also have about 10 degrees and clear sky Quote Link to comment Share on other sites More sharing options...
Question
Parma2015
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
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.