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-15-2012, 05:47 AM   PM User | #1
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 need this programmed...

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...
Code:
<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!
Durakken is offline   Reply With Quote
Old 11-15-2012, 05:54 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
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.
__________________
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
Old 11-15-2012, 06:07 AM   PM User | #3
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
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.
Durakken is offline   Reply With Quote
Old 11-15-2012, 06:34 AM   PM User | #4
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Old Pedant View Post
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

Quote:
However, most branches of the Eastern Orthodox Church still use the Julian calendar for calculating the dates of moveable feasts, including Easter (Pascha). Some Orthodox churches have adopted the Revised Julian calendar for the observance of fixed feasts, while other Orthodox churches retain the Julian calendar for all purposes.[1] The Julian calendar is still used by the Berber people of North Africa, and on Mount Athos.
Quote:
Originally Posted by Old Pedant View Post
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.
minder is offline   Reply With Quote
Old 11-15-2012, 09:23 PM   PM User | #5
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
Quote:
Originally Posted by minder View Post
From wikipedia

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.
Durakken is offline   Reply With Quote
Old 11-15-2012, 10:31 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
Trouble is, JavaScript will *NOT* display Feb 29, 1900 or Feb 29, 2100.

If you do
Code:
    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.
__________________
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
Old 11-16-2012, 12:09 AM   PM User | #7
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
Quote:
Originally Posted by Old Pedant View Post
Trouble is, JavaScript will *NOT* display Feb 29, 1900 or Feb 29, 2100.

If you do
Code:
    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
Durakken is offline   Reply With Quote
Old 11-16-2012, 01:55 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
Leap year is easy:

Code:
    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.
__________________
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
Old 11-16-2012, 01:56 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
This isn't close to right:
Code:
DayOfYear = days - (year*365)
That ignores any leap years that have already occurred!
__________________
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
Old 11-16-2012, 03:13 AM   PM User | #10
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
Quote:
Originally Posted by Old Pedant View Post
Leap year is easy:

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

Quote:
Originally Posted by Old Pedant View Post
This isn't close to right:
Code:
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
Durakken is offline   Reply With Quote
Old 11-16-2012, 05:23 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
*sigh*
Code:
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.
__________________
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
Old 11-16-2012, 11:42 PM   PM User | #12
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 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 ^.^

Code:
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);
};

Last edited by Durakken; 11-16-2012 at 11:45 PM..
Durakken is offline   Reply With Quote
Old 11-17-2012, 12:34 AM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
Okay, I surrender.

Here...this seems to work:
Code:
<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>
__________________
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
Old 11-17-2012, 01:41 AM   PM User | #14
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
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.
Durakken is offline   Reply With Quote
Old 11-17-2012, 02:13 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
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
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
Code:
    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.
__________________
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
Users who have thanked Old Pedant for this post:
Durakken (11-17-2012)
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 05:48 AM.


Advertisement
Log in to turn off these ads.