PDA

View Full Version : BC Years Without A Minus Sign

TimeTracker
07-22-2008, 12:26 AM
I have Julian or Old Western calendar calculators where to type in a B.C. year one year must be subtracted from the nominated year and a minus (-) sign placed before it. As yet I have never seen any JavaScript code where one can select a radio button indicating BC or AD (BCE or CE) and just type in the normal value. If there is some, I will be happy to look at it. If not, can you please help me work it out? I have code to switch between Julian and Gregorian astronomical algorithms, but not BC and AD. The calculators will soon be given a workout on the Internet and the simpler they are, the better for the end user, including myself.:)

Jesdisciple
07-22-2008, 05:14 AM
I don't understand what the problem is... Just make a checkbox or 2 radio buttons and multiply the year by -1 as necessary.

Maybe it would help for us to see your existing code?

TimeTracker
07-22-2008, 09:40 AM
JesDisciple - I apologise for not providing some code. I have wondered for years whether it is possible. None of the Julian calendar JavaScript calculators that I have seen have this facility and so I thought it might be too difficult to code. But as I have found out Dynamic Forum can do some pretty amazing things with JavaScript.

This is the source code for the calculator. I have since modernised it, but this was the original given to me in 2000. My focus is on 'var year'. This is how I am thinking.

Perhaps an if...else statement might be helpful. If not one, then the other.
AD will be the value in the text field input manually (selectedIndex is not practical)
BC will be the value in text field minus 1, yet it must also be negative. To create that would involve multiplying it by -1. 1 BC would then be year 0. 2 BC = 2 - 1 * -1 = 1
var year would be either value, which means the code would appear first as a separate function or included with the present function. Would further text fields (hidden) be required to assist with the calculations?

Also is there a way to allow only one radio button to be selected if a wrong decision is made and the other selected as well? In my browser both appear clicked.

<!--
function determineModuleAndWeekday(form) {
// Calculate Julian Day.
var day = Number(form.day.value);
var month = Number(form.month.value);
var year = Number(form.year.value); // this must be the final outcome;
var jd = gregorianDate2JulianDate(day, month, year);
form.julianDay.value = jd;

etc.
}
// -->

This is in the <BODY> section:

<form name="moduleForm">
<table>
<tr>
<th ALIGN=LEFT><font color="#000000">Enter Julian or Old Western Style
Date:-</font></th>
</tr>

<tr>
<td><input type="text" name="day" size=10><font color="#000000">(day [1..31])</font></td>
</tr>

<tr>
<td><input type="text" name="month" size=10><font color="#000000">(month
[1..12])</font></td>
</tr>

<tr>
(year [include the century])</td>
</tr>

<tr>
<td ALIGN=LEFT><input type="button" value="Calculate"
onClick="determineModuleAndWeekday(document.moduleForm)"><input TYPE="RESET" VALUE=" Reset "ONCLICK="putFocus(0,0)" "WIDTH="50"></td>
</tr>
</form>
</table>

TimeTracker
07-22-2008, 09:56 AM
2 BC = 2 - 1 * -1 = -1 (my oversight).
Also, does there have to be two radio buttons if the AD value does not need changing? Only if BC is selected would there be a need for the function. Otherwise the calculations should default to AD if the BC radio button is not selected. var year text field would obviously remain hidden [type="hidden"]. Your suggestions will be much appreciated.

Jesdisciple
07-22-2008, 04:44 PM
JesDisciple - I apologise for not providing some code. I have wondered for years whether it is possible. None of the Julian calendar JavaScript calculators that I have seen have this facility and so I thought it might be too difficult to code. But as I have found out Dynamic Forum can do some pretty amazing things with JavaScript.That functionality was probably excluded just because most people don't need/want it rather than due to the additional complexity or difficulty of writing it.

This is the source code for the calculator. I have since modernised it, but this was the original given to me in 2000.The modernized source would have been more appropriate, but if you haven't changed what you supplied it should be alright. (Note that I'm not complaining about the fragment - just the age.)

Perhaps an if...else statement might be helpful. If not one, then the other.Yes, but I'm using an abbreviated if...else known as a conditional (or ternary - "three-part") statement. This is handy when only the value of one variable depends on the condition - and conditional statements can be nested inside each other, but each should be parenthesized if it's not the entire statement. Here's the syntax:
var x = boolean ? ifBooleanIsTrue : ifBooleanIsFalse;

AD will be the value in the text field input manually (selectedIndex is not practical)... What's "selectedIndex"? EDIT: Oh, I now understand; never mind.

var year would be either value, which means the code would appear first as a separate function or included with the present function. Would further text fields (hidden) be required to assist with the calculations?I'm not understanding... BC vs AD is decided by the radio buttons, right?

Also is there a way to allow only one radio button to be selected if a wrong decision is made and the other selected as well? In my browser both appear clicked.Give each set of radio buttons the same 'name' attribute value (as I have below) and the browser will make them work like that.

This is also required for me to identify the radio buttons as a single set in the JavaScript (using the mechanism you already have, which I prefer).

<!--
function determineModuleAndWeekday(form) {
// Calculate Julian Day.
var day = Number(form.day.value);
var month = Number(form.month.value);
var year = form.years.value == 'BC' ? (Number(form.year.value) - 1) * -1 : Number(form.year.value);
var jd = gregorianDate2JulianDate(day, month, year);
form.julianDay.value = jd;

etc.
}
// -->

<form name="moduleForm">
<table>
<tr>
<th ALIGN=LEFT><font color="#000000">Enter Julian or Old Western Style
Date:-</font></th>
</tr>

<tr>
<td><input type="text" name="day" size=10><font color="#000000">(day [1..31])</font></td>
</tr>

<tr>
<td><input type="text" name="month" size=10><font color="#000000">(month
[1..12])</font></td>
</tr>

<tr>
(year [include the century])</td>
</tr>

<tr>
<td ALIGN=LEFT><input type="button" value="Calculate"
onClick="determineModuleAndWeekday(document.moduleForm)"><input TYPE="RESET" VALUE=" Reset "ONCLICK="putFocus(0,0)" "WIDTH="50"></td>
</tr>
</form>
</table>

Also, does there have to be two radio buttons if the AD value does not need changing? Only if BC is selected would there be a need for the function. Otherwise the calculations should default to AD if the BC radio button is not selected.I implemented this in the conditional statement above.

var year text field would obviously remain hidden [type="hidden"]. Your suggestions will be much appreciated.Don't you need to know the number of years whether they're BC or AD? Why do you need to hide the 'year' text field? (I'm not even sure JavaScript can change a field's type...)

TimeTracker
07-22-2008, 09:41 PM
JesDisciple, I think the code needs some improvement. The JavaScript is actually looking for a negative value in the text field. At the moment the result is always the same with either radio button selected, even when I place a minus sign before a BC year less 1. In other words, the radio buttons appear to be just window dressing. Is there a snippet of code perhaps which can be added onto var year to make this happen?
I would like to update the code, but it is not something that I want to do publicly here. In brief it involves the Twenty-Four Thrones of Rev. 4:4. I am currently defining the kinds of influences which they have on human thinking by matching language and concepts to the passage of time. This system was discovered from the canonical Gospels and Acts and helps make it possible to date the sayings and events recorded by them. Next year, God willing, the profiles and calculators will be ready for public use and discussion.

Jesdisciple
07-23-2008, 04:30 AM
I'm guessing that you're replying to this comment:
The modernized source would have been more appropriate, but if you haven't changed what you supplied it should be alright. (Note that I'm not complaining about the fragment - just the age.)I don't mean that you should supply the entire source, but that what you do supply should be current - mostly so you don't have to work at merging my source with yours.

And you're correct that my code doesn't work. That's very strange, as I was sure that JS handles radio buttons that way. Oh well, there I go again trusting my memory rather than testing my code...

I Googled the issue and got a nifty function from http://www.somacon.com/p143.php:

return "";
}
}else{
return "";
}
}
for(var i = 0; i < radioLength; i++) {
}
}
return "";
}Put that in your script and use this line rather than the one I gave before:

var year = getCheckedValue(form.years) == 'BC' ? (Number(form.year.value) - 1) * -1 : Number(form.year.value);

P.S. I'd like to see your results when you're through. :)

TimeTracker
07-23-2008, 06:44 AM
JesDisciple, you are a gem and very special in His eyes. I had already modernised the code (it had to be done) and, when I put your scripts in, it worked perfectly first time. So there we have a solution. Below is the successful version. I would like to know which is the correct way to acknowledge Dynamic Drive. Usually I put visible copyright and credit notices in a file which opens up in a smaller window from a conspicuous link on the page, there being so many to acknowledge and thank. A few of my scripts exceed 2000 lines. However, I have no objection to putting a button on the page if that is necessary.

// Radio button code provided by Dynamic Drive (http://www.dynamicdrive.com/forums/;
// function obtained from http://www.somacon.com/p143.php/;

return "";
}
}else{
return "";
}
}
for(var i = 0; i < radioLength; i++) {
}
}
return "";
}

function determineModuleAndWeekday(df) {
// Calculate Julian Day.
var day = parseFloat(df.day.value);
var month = parseFloat(df.month.value);
var year = getCheckedValue(df.years) == 'BC' ? (eval(df.year.value) - 1) * -1 : parseFloat(df.year.value);
var jd = gregorianDate2JulianDate(day, month, year);
df.julianDay.value = jd;

<form>
<table>
<tr>
<th ALIGN=LEFT><font color="#000000">Enter Julian or Old Western Style
Date:-</font></th>
</tr>

<tr>
<td><input type="text" name="day" size=10><font color="#000000">(day [1..31])</font></td>
</tr>

<tr>
<td><input type="text" name="month" size=10><font color="#000000">(month
[1..12])</font></td>
</tr>

<tr>
</tr>

<tr>
<td ALIGN=LEFT><input type="button" value="Calculate"
onClick="determineModuleAndWeekday(this.form)"><input type="reset" VALUE=" Reset "></form></td>
</tr>
</table>

Jesdisciple
07-23-2008, 07:07 AM
Thanks. :)

Attribution is technically not required for help from the forums, but that looks fine to me.

However, you have a problem with semicolons and I can't think of any other way to help it than by dumping a lecture on you. :(

Semicolons aren't actually required in JavaScript except in special cases. But I make it a habit to include them based on preference and anticipation that they may be required someday because of the trend among other languages.

Particularly, semicolons are never required inside comments.
//Line comments terminate at the newline.
/*Multiline comments terminate at the asterisk-slash (below).
This entire chunk of code is valid in nearly all major languages
(including the ones that require semicolons after each statement).*/

Because of the semicolon at the end of your DD attribution above, clicking the first link in my browser (or any text editor that supports links) doesn't work very well. (The other site somehow ignores the semicolon.)

TimeTracker
07-23-2008, 11:57 AM
Thanks for those comments. I was doing fine until I purchased AceHtml Pro 6, thinking I was doing myself a service. That puts semi-colons everywhere. If you do not have enough, then you have fatal errors. I used to follow what other coders were doing, such as <!-- or // text --> or a full stop, never a semicolon. I like your style and will adopt it forthwith.
The AD/BC code is working fine - like a dream. Thanks again for everything.

riptide
07-24-2008, 11:40 AM
you could use dreamweaver and you don't have to get the new one that is out. dream weaver 8 is fine enough for me. but it only last for 30 days. I'm not sure is you can still buy dream weaver 8