...

View Full Version : I need this programmed...



Durakken
11-15-2012, 05:47 AM
Yeah... I haven't programmed in a while and really don't want to relearn everything just to do this simple thing so could someone please do this for me?

I need a program where I can stick in a number and it gives me a set of 3 dates. 2 of them I can do because i made them up while the third is the julian calender more or less.

So I need to be able to input a number
then I need to do work on that number to get what year day and month it would be if day 1 is "Jan 1, 1" the year is 365.25 days long (which means every 4th year is a leap year, and I need to be able to put in a number of days up to 20,000,000

I know there is probably some easy trick to doing this but I've never been good with those methods and like i said i haven't programmed in a while so could someone write this up? Nothing fancy... it's for personal quick conversion between these dates.

Basically it needs to be a function that accepts a number up to 20,000,000 and returns write a line of "Julian Calender: Month Day, Year NY" printed on the page...

Basically fit in this...


<html>
<head>
<script type="text/JavaScript">
<!--
function popup() {
var days = prompt("Days?", "");
document.write("Days: " +days);
JulienCal(days);

document.write("<br /><input type=button onclick=popup() value=popup>");
};
//-->
</script>
</head>
<body>
<input type="button" onclick="popup()" value="popup">
</body>
</html>


Thanks for the help!

Old Pedant
11-15-2012, 05:54 AM
Ummm...you can NOT use document.write after a page has loaded. When you do, you wipe out all content on the page except what you just wrote.

And not to ask a silly question, but WHY do you want something so incredibly inaccurate? Nobody uses a Julian calendar any more. What possible real world use could it have?

And the other problem is that using a calendar as you have asked for could easily result in illegal dates. e.g., you could easily end up with FEBRUARY 29, 1900, just to pick the obvious example.

Durakken
11-15-2012, 06:07 AM
document.write doesn't do that... well on different browsers it doesn't... not really important as i can adjust that.

I know the other things. I considered it and decided to go with this style calender just for the overall simplicity for what i am using it for as the corrections made via the gregorian calender for this particular thing i can just shove aside. Also i want 1900 feb 29 on there... for my purposes that is accurate.

edit... oh wait you mean when i hit the button the button goes away. i know. doesn't matter for this. It works fine how i have it in the code i showed.

minder
11-15-2012, 06:34 AM
And not to ask a silly question, but WHY do you want something so incredibly inaccurate? Nobody uses a Julian calendar any more. What possible real world use could it have?


From wikipedia (http://en.wikipedia.org/wiki/Julian_calendar)


However, most branches of the Eastern Orthodox Church (http://en.wikipedia.org/wiki/Orthodox_Church) still use the Julian calendar for calculating the dates of moveable feasts (http://en.wikipedia.org/wiki/Moveable_feast), including Easter (http://en.wikipedia.org/wiki/Easter) (Pascha). Some Orthodox churches have adopted the Revised Julian calendar (http://en.wikipedia.org/wiki/Revised_Julian_calendar) for the observance of fixed feasts (http://en.wikipedia.org/wiki/Fixed_feast), while other Orthodox churches retain the Julian calendar for all purposes.[1] (http://en.wikipedia.org/wiki/Julian_calendar#cite_note-1) The Julian calendar is still used by the Berber people (http://en.wikipedia.org/wiki/Berber_people) of North Africa (http://en.wikipedia.org/wiki/North_Africa), and on Mount Athos (http://en.wikipedia.org/wiki/Mount_Athos).



And the other problem is that using a calendar as you have asked for could easily result in illegal dates. e.g., you could easily end up with FEBRUARY 29, 1900, just to pick the obvious example.

Since the op wants to use the Julian calendar then 1900 is a leap year. My understanding is that in the Julian calendar the only criteria for a leap year is that it be divisible by 4.

Durakken
11-15-2012, 09:23 PM
From wikipedia (http://en.wikipedia.org/wiki/Julian_calendar)

Since the op wants to use the Julian calendar then 1900 is a leap year. My understanding is that in the Julian calendar the only criteria for a leap year is that it be divisible by 4.

Yes. The Julian system is "more or less" 365.25 days system with a whole bunch of odd things that happened to it. The gregorian system is 365.2425 or some where close to that, making only centuries divisible by 4 leap years, removing 3 days out of the overall cycle.

The reason for the change was to get the seasons right. The reason I need this is for personal use to convert dates between 3 systems (or perhaps more if I come up with needing more) Some of the systems care about keeping seasons right and some don't. The calenders are for a conworld and I am going with the conceit that the world has a perfect 365.25 year. That way i can make it simple but still keep it close to reality. It's a nice little compromise.

I'm also having a hard time with one of the other calenders cuz it's weird... each week has 7 days and each of month 7 weeks and there are 7 months. At the end of 7 years there is a year of 7 months of 21 days and then a "non" year of 9 days. This system and the Julian system lines up every 28 years having the same number of days. I thought that that would make it easier to calculate longer scale numbers but, meh, i still don't like doing it.

Old Pedant
11-15-2012, 10:31 PM
Trouble is, JavaScript will *NOT* display Feb 29, 1900 or Feb 29, 2100.

If you do

var theDate = new Date( 1900, 1, 29 ); // 1 is February in JS code
document.write( theDate );

JavaScript *will* display "March 1, 1900" back at you.

So in order to display "Feb 29, 1900" you will have to not use JavaScript's Date() object *AT ALL* and essentially re-create the entire calendar system.

Of course it can be done. It's just a real pain in the patootie and you are asking a lot to have somebody write that all for you.

Durakken
11-16-2012, 12:09 AM
Trouble is, JavaScript will *NOT* display Feb 29, 1900 or Feb 29, 2100.

If you do

var theDate = new Date( 1900, 1, 29 ); // 1 is February in JS code
document.write( theDate );

JavaScript *will* display "March 1, 1900" back at you.

So in order to display "Feb 29, 1900" you will have to not use JavaScript's Date() object *AT ALL* and essentially re-create the entire calendar system.

Of course it can be done. It's just a real pain in the patootie and you are asking a lot to have somebody write that all for you.

what I was thinking is doing this

year = days/365.25
Array MonthEnd(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365)
DayOfYear = days - (year*365)

if year is divisible by 4 leap = yes (i don't know how to code that easily)
if leap
for i=1; i<12; i++
MonthEnd(i) = MonthEnd(i)+1

Switch
DayOfYear < MonthEnd[1]
Month = Jan
DayOfYear > MonthEnd[1] && DayOfYear < MonthEnd[2]
Month = Feb
Date = DayOfYear - MonthEnd[1]

Doc.write(Year Month Date)

Does this logic pan out? I'm not sure how switch works and I haven't gotten for loop to work for some reason when i tried it for something else

Old Pedant
11-16-2012, 01:55 AM
Leap year is easy:



var leap = ( year % 4 ) == 0;

Code looks basically right, but I don't see any need for a switch if done right.

Instead, just have an array of month names and index into the array by the month number.

Old Pedant
11-16-2012, 01:56 AM
This isn't close to right:


DayOfYear = days - (year*365)

That ignores any leap years that have already occurred!

Durakken
11-16-2012, 03:13 AM
Leap year is easy:



var leap = ( year % 4 ) == 0;

Code looks basically right, but I don't see any need for a switch if done right.

Instead, just have an array of month names and index into the array by the month number.

I only see if-else or switch as a viable way of doing it less there is something. The if-else/switch is needed to select the month in general. There is no other place that that month selection takes place and I also have to remove excess days so it fits to the month.

I don't see another way to do this. I'll use an array for month names, but i don't see how that helps selecting months


This isn't close to right:


DayOfYear = days - (year*365)

That ignores any leap years that have already occurred!

year/4 can give the number of leap years, but I don't know where to add it

Old Pedant
11-16-2012, 05:23 AM
*sigh*


var mDays = [ 31,28,31,30,31,30,31,31,20,31,30,31 ];
// handle leap year:
mDays[1] = isLeapYear ? 29 : 28;

var theMonth = -1;
for ( var m = 0; m < 12; ++m )
{
if ( days <= mDays[m] )
{
theMonth = m;
break; // out of for loop
}
days -= mdays{m];
}
var monthName = names[theMonth];

But there is a problem with that, as you will discover.

Logically, day ZERO of each year should be Jan 1st.

Probably you can handle that by first subtracting off the number of days that correspond to the correct year and then adding back 1 day before entering the above code. Probably. Untested.

Durakken
11-16-2012, 11:42 PM
I figured out this part. Thanks for the help... now to work on the next one... which I hope i can figure out

Here's the code i ended up with... I know it's not all that pretty, but it seems to be working

also yes i know it's probably better if i used your way, Old Pedant, to calculate the date, but i already had this way so I just used it. I probably would switch it if it really mattered ^.^



function JulienCal(days) {
jyear = Math.floor(days/365.25)
var jMonthEnd = new Array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365)
var jMonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
DaysNotOfYear = jyear*365.25
DaysNotOfYear = DaysNotOfYear.toFixed()
DayOfYear = days - DaysNotOfYear
jyear++
if(leap = jyear % 4 == 0) {
for (i=1; i<12; i++) { jMonthEnd[i] = jMonthEnd[i]+1}}

theMonth = 0
for (m = 0; m < 12; m++) {
if(DayOfYear <= jMonthEnd[m]) {
theMonth = m;
break;}
}

if(theMonth > 0) {
jDate = DayOfYear - jMonthEnd[theMonth-1]
}else{
jDate = DayOfYear
}

document.write("<br />Julian Calender: " +jMonth[theMonth]+ " " +jDate+ ", " +jyear);
};

Old Pedant
11-17-2012, 12:34 AM
Okay, I surrender.

Here...this seems to work:


<html>
<body>
<form id="theForm" onsubmit="return false;">
Number of days: <input id="days" name="days" onchange="getJulianDate();" />
<br/>
<input type="button" value="Calculate" onclick="getJulianDate();" />
</form>
<hr/>
Julian date: <span id="jDate"></span>

<script type="text/javascript">
function getJulianDate( )
{
var nDays = Number( document.getElementById("days").value );

var quads = Math.floor( (nDays-1) / 1461 ); // 1461 days in 4 years
nDays -= quads * 1461;

var yrs = 4 * quads;

if ( nDays > 366 ) { ++yrs; nDays -= 366; }
if ( nDays > 365 ) { ++yrs; nDays -= 365; }
if ( nDays > 365 ) { ++yrs; nDays -= 365; }

if ( nDays > 365 ) { alert("Algorithm failed!"); return "Error"; }

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 =
monthName + " " + nDays + ", " + yrs;
}
</script>
</body>
</html>

Durakken
11-17-2012, 01:41 AM
Why would you do that? People like you infuriate me.
I spent the last few days trying to figure this out while I asked for help, but instead got you being arrogant and then when I get it all figured out and working "oh now i'll give you what i could have done days ago." You just wasted both our times by doing that and one has to wonder about the motives behind your actions.

Old Pedant
11-17-2012, 02:13 AM
OH MAN! I *DO* apologize!

I actually had *NOT* seen your post from today!

I had looked at the thread earlier in the day and realized I had a typo in the code I showed you. So I fixed it and then checked to make sure it worked.

And then I got to thinking about the problem and realized that the hard part was that, as I had mentioned, you need a zero-based number of days when dividing by years but you want a 1-based number of days for the days in months. That's when I hit on doing

var quads = Math.floor( (nDays-1) / 1461 ); // 1461 days in 4 years
nDays -= quads * 1461;


It was more a matter of surrendering to my own stubborness than anything else.

Even AFTER I posted I didn't scroll up and see your post with the right answer!

PLEASE...it was just blindness and my own stubbornness at finally understanding the meat of the problem that drove me to write the code.

I have *STILL* not tested your answer. I am assuming it works and you have tested it.

And here I was so happy at myself for coming up with that "quads" trick.

Durakken
11-17-2012, 03:50 AM
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


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


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


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.

Durakken
11-17-2012, 10:44 PM
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


<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>

Old Pedant
11-19-2012, 08:32 PM
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:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum