/    Sign up×
Community /Pin to ProfileBookmark

Weekday From Precalculated JD Number

I need some help please. What is wrong with my “var weekDay[i]” code that it does not select the weekday for day 5 or work for any other decimal and fraction value? The JD Number has been precalculated.
<html>
<head>
<script LANGUAGE=”JavaScript”>

<!– Original: Ryan Sokol –>
<!– Web Site: The JavaScript Source –>
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! [url]http://javascript.internet.com[/url] –>

<!– Begin
function checkInt(str) {
if (!str) return 0;
var ok = “”;
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1);
if (ch < “0” || “9” < ch) {
return parseInt(ok);
}
else ok += ch;
}
return parseInt(str);
}

function checkDecimal(str) {
if (!str) return 0;
var ok = “”;
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1);
if ((ch < “0” || “9” < ch) && ch != ‘.’) {
return parseFloat(ok);
}
else ok += ch;
}
return str;
}

function getWeekDay(df) {

var numWk = df.numWk.value; // may have decimal and fraction –>
var resultA = df.resultA.value; // weeks as integer –>
var resultB = df.resultB.value; // day(s) as integer –>
numA = (checkDecimal(df.numWk.value)); // validates input
if (numA) {
df.resultA.value = parseInt(numA);
numA -= parseInt(numA); numA *= 7;
df.resultB.value = parseInt(numA);
numA -= parseInt(numA); numA *= 7; [COLOR=”Red”]// works fine up to this point if lines 2 to 4 below are excised –>[/COLOR]
}

[COLOR=”Red”]var weekDay[i] = newArray(“Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”);
for (var i = 0; i < 7; i++);
if (df.resultB.value = i) {df.weekDay.value = “newArray[i]“}
[/COLOR]

}

// –>
</script>
</head>
<body>

<p>
<form><input type=”text” name=”numWk” value=”350621.85714″ size=”10″>
<input type=”button” value=”Get Weekday” onClick=”getWeekDay(this.form)”>
<input type=”text” name=”resultA” size=”5″>
<input type=”text” name=”resultB” size=”5″>
<input type=”text” name=”weekDay” size=”10″></form>

</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TimeTrackerauthorJun 02.2007 — I found my own simple solution:

Replace the red text with the following simple code and it works fine:

if (df.resultB.value == 0) { df.weekDay.value = "Monday";}

if (df.resultB.value == 1) { df.weekDay.value = "Tuesday";}

if (df.resultB.value == 2) { df.weekDay.value = "Wednesday";}

if (df.resultB.value == 3) { df.weekDay.value = "Thursday";}

if (df.resultB.value == 4) { df.weekDay.value = "Friday";}

if (df.resultB.value == 5) { df.weekDay.value = "Saturday";}

if (df.resultB.value == 6) { df.weekDay.value = "Sunday";}

Thanks, team! Now it is so easy to calculate the weekday from a Julian Day Number.
Copy linkTweet thisAlerts:
@TimeTrackerauthorJun 02.2007 — Actually there is a much simpler way to do the above without the parseInt.

function gregorian2JulianDay(){

var d=parseFloat(document.form.date.value);

var m=parseFloat(document.form.month.value);

var y=parseFloat(document.form.year.value);

if (m<3){
var y=y-1;
var m=m+12;
}
var a=Math.floor(y/100);
var b=2-a+Math.floor(a/4);
document.form.JDNumA.value=eval(Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+d+b-1525);

}

function julianDay2Gregorian(){

var z = document.form.JDNumA.value;
var b = z%7;
if (b==0) {e="Tue.,"};
if (b==1) {e="Wed.,"};
if (b==2) {e="Thu.,"};
if (b==3) {e=" Fri.,"};
if (b==4) {e="Sat.,"};
if (b==5) {e="Sun.,"};
if (b==6) {e="Mon.,"};
document.form.weekDayA.value=e


The result is posted to a form text box and matches all calendar dates tested. Please do not ask why it begins with Tuesday. That is an adjustment I made. I also have 1525 instead of 1524 in my algorithm. Again, it works. The first two parts of the thread can be ignored.
Copy linkTweet thisAlerts:
@JMRKERJun 02.2007 — [code=php]
function julianDay2Gregorian(){
var zdays = new Array('Tue.','Wed.','Thu.','Fri.','Sat.','Sun.','Mon.');
var z = document.form.JDNumA.value;
var b = z%7;
document.form.weekDayA.value=zdays[b];
...
[/code]

Might simplify the code a bit without a change to the functionality. ?

Or even:

var z = document.form.JDNumA.value;

document.form.weekDayA.value=zdays[z % 7];

to save 1 variable.
×

Success!

Help @TimeTracker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...