Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2012, 03:50 AM   PM User | #16
Durakken
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Durakken is an unknown quantity at this point
I see. No problem.

Perhaps you can help fix with this other one that i almost have working.
7 days = 1 week
7 weeks = 1 month
7 months = 1 year (343 days)
the 8th year is 7 months of 21 days
the 9th year is 9 days
After the 9th year it gets cycled into a new cycle where the years start to recount

This cycle system lines up so that at the end of the 28th year in the julian system ends on the same day as the 4th cycle of this system with 10227 days.

the current problem i'm having is that 2401 days should be the last day of the 7th year... It is returning a year 8 day 0
edit - I found the same problem in my julien calender code... The last day of every year is zero'd out.

this is what i have... yeah i know my naming conventionbs are sloppy v.v
Code:
function DrCal(days) {
drCycle = 2557
drYeara = 343
drYearb = 147
//drYearc = 9


Cycle = Math.floor(days/drCycle)
DaysNotOfCycle = Cycle * drCycle
DaysOfCycle = days - DaysNotOfCycle
var drMonth=new Array("m1","m2","m3","m4","m5","m6","m7")
var drMonthEnd=new Array("49","98","147","196","245","294","343")

var drMonthb=new Array("m1","m2","m3","m4","m5","m6","m7")
var drMonthbEnd=new Array("21","42","63","84","105","126","147")

if(DaysOfCycle <= 2401) {
	drYear = Math.floor(DaysOfCycle / drYeara)
	DaysNotOfYear = drYear*drYeara // causing problem
	drYear++
	DaysOfYear = DaysOfCycle - DaysNotOfYear //causing problem
	theMonth = 0
	for (m = 0; m < 7; m++) {
		if(DaysOfYear <= drMonthEnd[m]) {
			theMonth = m;
			break;}
	}
	Month = drMonth[theMonth]
	if(theMonth > 0) {
		drDate = DaysOfYear - drMonthEnd[theMonth-1]
	}else{
		drDate = DaysOfYear
	}
} else if ((DaysOfCycle > 2401) && (DaysOfCycle <= 2548)) {
	drYear = 8
	DaysOfYear = DaysOfCycle - (drYeara*7)

	theMonth = 0
	for (m = 0; m < 7; m++) {
		if(DaysOfYear <= drMonthbEnd[m]) {
		theMonth = m;
		break;}
	}
	Month = drMonthb[theMonth]
	if(theMonth > 0) {
		drDate = DaysOfYear - drMonthbEnd[theMonth-1]
	}else{
		drDate = DaysOfYear
	}
}else{
	drYear = 9
	drDate = DaysOfCycle - (drYeara+drYearb)
	Month = 1
}

Cycle++

document.write("<br />Dr Cycle: " +Cycle+ " Year:" +drYear+ " Month:" +Month+ " " +drDate)
}
edit again...
I have figured a solution to my problem. I switched to your code, Pedant, and add 3 changes... 1 non-important, 1 I moved the function to a subfunction so i can call another date system, and I corrected one minor little problem
Code:
    var yrs = (4 * quads) +1;
This makes the date start at year 1 rather than year 0
Everything else as far as I have tested works great and I am using the way you coded this one to try to code the other one i mentioned...

here's the code so far
Code:
    var nDays = Number( document.getElementById("days").value );

    var Cycle = Math.floor(nDays / 2557);
    if(nDays%2557 != 0) 
    {
        nDays -= Cycle*2557;
        Cycle++;
    }


/*    var mnames = ["Jan","Feb","Mar","Apr","May","Jun",
              "Jul","Aug","Sep","Oct","Nov","Dec" ];

    var mDays = [ 31,28,31,30,31,30,31,31,30,31,30,31 ];
    // handle leap year:
    mDays[1] = ( yrs % 4 == 0 ) ? 29 : 28;

    var theMonth = -1;
    for ( var m = 0; m < 12; ++m )
    {
        if ( nDays <= mDays[m] )
        {
            theMonth = m;
            break; // out of for loop
        }
        nDays -= mDays[m];
    }
    var monthName = mnames[theMonth];
*/
    document.getElementById("jDate").innerHTML = 
        "Cycle: " +Cycle+ " Days: " +nDays;
I think for the years i'm going to use a the month code as it should be more or less the same

I do however have a question... when I merge these two together, will the var names cause a problem. I forget how var localization in JS works.

Last edited by Durakken; 11-17-2012 at 09:26 AM..
Durakken is offline   Reply With Quote
Old 11-17-2012, 10:44 PM   PM User | #17
Durakken
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Durakken is an unknown quantity at this point
Grrr... so I got the next one all programmed out... and now I'm stuck yet again...
Whenever a multiple of 2557 is entered the date returned is wrong and this seems to push the date off a little.

Here's the code. I'm pretty sure it has something to do with the fix I tried with nDays%2557

edit: ^.^ I figured out what the problem was sort of, (and edited it into the code) but I also forgot something... which kinda throws a wrench into this whole thing like the leap year thing did... This system is supposed to have a reverse leap year which is why 10227 is supposed to align with the Julian Calender. The reverse leap is supposed to be that last day off the 4th Cycle. Is there any way to fudge this so that that happens and is still accurate or is do I have to refigure this out?

edit2: never mind...fixed ^.^ I just used day subtraction after i realized that if i subtract 10227 from 10228 it'll restart with 1 so i just needed a way to keep track of the cycles after that. yar
Code:
<html>
<body>
<form id="theForm" onsubmit="return false;">
Number of days: <input id="days" name="days" onchange="getDate();" />
<br/>
<input type="button" value="Calculate" onclick="getDate();" />
</form>
<hr/>
Dr date: <span id="jDate"></span>

<script type="text/javascript">
function getDate()
{
    getJulianDate()
}

function getJulianDate( )
{
    var nDays = Number( document.getElementById("days").value );

    var Cycle = Math.floor(nDays / 2557);
    if(nDays%2557 != 0) 
    {
        nDays -= Cycle*2557;
        Cycle++;
    } else {
        nDays = 2557;
    }

    var years = [343,343,343,343,343,343,343,147,9];
    theYear = 0;
    for ( var y = 0; y < 9; ++y)
    {
        if ( nDays <= years[y] )
        {
            theYear = y +1;
            break;
        }
        nDays -= years[y]
    }

    var mnames = ["Darusitar","Bendratar","Enzer","Mizutar","Genutar","Krinzitar",
              "Niemetar"];
    var mDays = [ 49,21 ];
    var s = 0;
    if (theYear >= 8) { s = 1; }

    var theMonth = -1;
    for ( var m = 0; m < 7; ++m )
    {
        if ( nDays <= mDays[s] )
        {
            theMonth = m;
            break; // out of for loop
        }
        nDays -= mDays[s];
    }

    if ( theYear == 9)
    {
        var monthName = "Morgetar";
    } else {
        var monthName = mnames[theMonth];
    }
    
    document.getElementById("jDate").innerHTML = 
        monthName+ " " +nDays+ ", " +theYear+"."+Cycle+ "T";
}
</script>
</body>
</html>

Last edited by Durakken; 11-18-2012 at 01:31 AM..
Durakken is offline   Reply With Quote
Old 11-19-2012, 08:32 PM   PM User | #18
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I haven't go time to look further right now, but will be back later.

In the meantime, it *SOUNDS* like the same problem I has with zero-based vs. 1-base years.

I *think* your answer will be to pull the same trick I did:
Code:
    var nines = Math.floor( ( nDays - 1 ) / 2557 );
    nDays -= nines * 2557;
    var yrs = 9 * nines;
And then use subtraction, as I did, to find the right year in the last set of 9 years,

I know it feels clumsy, but it solves the zero-based vs. one-based problem.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 AM.


Advertisement
Log in to turn off these ads.