...

View Full Version : Javascript Date Engine Ver1.1



Omega9850
04-02-2012, 03:50 AM
I've added a few cool new features to the Javascript Date Engine that I'm programming. The code is below...


//DATE ENGINE
function dateengine()
{
var dateengine=new Array(5);
dateengine[1]="1.1"; //Date Engine Version
dateengine[2]=new Date(); //Full Date: Day, Month, Day, Year, Military Time, Time Zone
dateengine[3]=dateengine[2].getHours(); //Military Hours (0-23)
dateengine[4]=dateengine[2].getMinutes(); //Minutes (0-59)
dateengine[5]=dateengine[2].getSeconds(); //Seconds (0-59)

if (dateengine[2].getTimezoneOffset() == 0)
{
localStorage.setItem("TimeZone","UTC+0:00");
}
else{}
if (dateengine[2].getTimezoneOffset() > 0)
{
if ((dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "" && (dateengine[2].getTimezoneOffset()/60).toString().charAt(2) == "")
{
localStorage.setItem("TimeZone","UTC-" + (dateengine[2].getTimezoneOffset()/60) + ":00" );
}
else {}
if ((dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "." && (dateengine[2].getTimezoneOffset()/60).toString().charAt(2) > 0 && (dateengine[2].getTimezoneOffset()/60).toString().charAt(3) == "")
{
localStorage.setItem("TimeZone","UTC-" + (dateengine[2].getTimezoneOffset()/60).toString().charAt(0) + ":" + (dateengine[2].getTimezoneOffset()/60).toString().charAt(2)*.6 + "0");
}
else {}
if ((dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "." && (dateengine[2].getTimezoneOffset()/60).toString().charAt(2) > 0 && (dateengine[2].getTimezoneOffset()/60).toString().charAt(3) > 0)
{
localStorage.setItem("TimeZone","UTC-" + (dateengine[2].getTimezoneOffset()/60).toString().charAt(0) + ":" + ((dateengine[2].getTimezoneOffset()/60).toString().charAt(2) + (dateengine[2].getTimezoneOffset()/60).toString().charAt(3))*.6);
}
else {}
}
else{}
if (dateengine[2].getTimezoneOffset() < 0)
{
if (Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "" && Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(2) == "")
{
localStorage.setItem("TimeZone","UTC+" + Math.abs(dateengine[2].getTimezoneOffset()/60) + ":00" );
}
else {}
if (Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "." && Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(2) > 0 && Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(3) == "")
{
localStorage.setItem("TimeZone","UTC+" + Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(0) + ":" + Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(2)*.6 + "0");
}
else {}
if (Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(1) == "." && Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(2) > 0 && Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(3) > 0)
{
localStorage.setItem("TimeZone","UTC+" + Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(0) + ":" + (Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(2) + Math.abs(dateengine[2].getTimezoneOffset()/60).toString().charAt(3))*.6);
}
else {}
}
else{}

if (dateengine[3] == 0)
{
sessionStorage.setItem("meridiem","AM");
sessionStorage.setItem("hour", dateengine[3] + 12);
}
else {}
if (dateengine[3] >= 1 && dateengine[3] <= 11)
{
sessionStorage.setItem("meridiem","AM");
sessionStorage.setItem("hour",dateengine[3]);
}
else {}
if (dateengine[3] == 12)
{
sessionStorage.setItem("meridiem","PM");
sessionStorage.setItem("hour",dateengine[3]);
}
else {}
if (dateengine[3] >= 13 && dateengine[3] <= 23)
{
sessionStorage.setItem("meridiem","PM");
sessionStorage.setItem("hour",dateengine[3] - 12);
}
else {}

if (dateengine[3] == 0)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 1)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 2)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 3)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 4)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 5)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 6)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 7)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 8)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 9)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 10)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 11)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 12)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 13)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 14)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 15)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 16)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 17)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 18)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 19)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 20)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 21)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 22)
{
//Any Hour Dependent Variables Here
}
else {}
if (dateengine[3] == 23)
{
//Any Hour Dependent Variables Here
}
else {}

var day=dateengine[2].getDate(); //Day (1-31)
var daynamedarray=new Array(7);
daynamedarray[1]="Sunday";
daynamedarray[2]="Monday";
daynamedarray[3]="Tuesday";
daynamedarray[4]="Wednesday";
daynamedarray[5]="Thursday";
daynamedarray[6]="Friday";
daynamedarray[7]="Saturday";
var daynamed=daynamedarray[dateengine[2].getDay()+1]; //Day (Saturday-Sunday)

if (daynamed == "Sunday")
{
sessionStorage.setItem("Yesterday","Saturday");
sessionStorage.setItem("Tomorrow","Monday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Monday")
{
sessionStorage.setItem("Yesterday","Sunday");
sessionStorage.setItem("Tomorrow","Tuesday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Tuesday")
{
sessionStorage.setItem("Yesterday","Monday");
sessionStorage.setItem("Tomorrow","Wednesday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Wednesday")
{
sessionStorage.setItem("Yesterday","Tuesday");
sessionStorage.setItem("Tomorrow","Thursday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Thursday")
{
sessionStorage.setItem("Yesterday","Wednesday");
sessionStorage.setItem("Tomorrow","Friday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Friday")
{
sessionStorage.setItem("Yesterday","Thursday");
sessionStorage.setItem("Tomorrow","Saturday");
//Any Day Dependent Variables Here
}
else {}
if (daynamed == "Saturday")
{
sessionStorage.setItem("Yesterday","Friday");
sessionStorage.setItem("Tomorrow","Sunday");
//Any Day Dependent Variables Here
}
else {}

var month=dateengine[2].getMonth()+1; //Month (1-12)
var monthnamedarray=new Array(12);
monthnamedarray[1]="January";
monthnamedarray[2]="February";
monthnamedarray[3]="March";
monthnamedarray[4]="April";
monthnamedarray[5]="May";
monthnamedarray[6]="June";
monthnamedarray[7]="July";
monthnamedarray[8]="August";
monthnamedarray[9]="September";
monthnamedarray[10]="October";
monthnamedarray[11]="November";
monthnamedarray[12]="December";
var monthnamed=monthnamedarray[month]; //Month (January-February)

if (monthnamed == "January")
{
sessionStorage.setItem("LastMonth","December");
sessionStorage.setItem("NextMonth","February");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "February")
{
sessionStorage.setItem("LastMonth","January");
sessionStorage.setItem("NextMonth","March");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "March")
{
sessionStorage.setItem("LastMonth","February");
sessionStorage.setItem("NextMonth","April");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "April")
{
sessionStorage.setItem("LastMonth","March");
sessionStorage.setItem("NextMonth","May");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "May")
{
sessionStorage.setItem("LastMonth","April");
sessionStorage.setItem("NextMonth","June");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "June")
{
sessionStorage.setItem("LastMonth","May");
sessionStorage.setItem("NextMonth","July");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "July")
{
sessionStorage.setItem("LastMonth","June");
sessionStorage.setItem("NextMonth","August");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "August")
{
sessionStorage.setItem("LastMonth","July");
sessionStorage.setItem("NextMonth","September");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "September")
{
sessionStorage.setItem("LastMonth","August");
sessionStorage.setItem("NextMonth","October");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "October")
{
sessionStorage.setItem("LastMonth","September");
sessionStorage.setItem("NextMonth","November");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "November")
{
sessionStorage.setItem("LastMonth","October");
sessionStorage.setItem("NextMonth","December");
//Any Month Dependent Variables Here
}
else {}
if (monthnamed == "December")
{
sessionStorage.setItem("LastMonth","November");
sessionStorage.setItem("NextMonth","January");
//Any Month Dependent Variables Here
}
else {}

var lastyear=dateengine[2].getFullYear()-1; //Last Year (XXXX)
var year=dateengine[2].getFullYear(); //Year (XXXX)
var nextyear=dateengine[2].getFullYear()+1; //Next Year (XXXX)

setTimeout("dateengine()",60000);
}
//DATE ENGINE END

Just like Ver1.0 it is compatible with major browsers.

Ver1.1 includes a system that determines the Time Zone of a client, without any server scripting!

To retrieve the Time Zone, refer to localStorage.getItem("TimeZone")

Any feedback would be much appreciated!

Nile
04-02-2012, 04:02 AM
I would use some switch()'s instead of a bunch of if()'s.

Dormilich
04-02-2012, 11:18 AM
your dateengine array length is 6, not 5 (JS uses 0-based array indexing) (same goes for all other arrays you use). but more importantly, do not use variable names twice (dateengine is the name of the data array and the function!). and I don’t see a need to use those arrays at all, plain local variables would be sufficient.

setTimeout(dateengine, 60000); (setTimeout() originally expects a function and falls back to evaluating strings) though that will fail in your current setup (see above).

there are unnecessary subsequent function calls (e.g. dateengine[2].getTimezoneOffset() is called eight times (!) consecutively)

if the else{} statement is empty, omit it.

Omega9850
04-02-2012, 10:07 PM
Thanks for the responses, Nile and Dormilich!
I'm not too familiar with switch(), although seeing some examples of them, I see how they would work. Thanks Nile, I'll be sure to use it in Ver1.2!

Yeah, I know that Arrays are rooted at 0. I guess it's a habit for me to use 1 instead, but I see how 0 is better. I guess I should change function dateengine() to function initiatedateengine(). As for dateengine[2].getTimezoneOffset() I see how using that would accommodate a lot of space after a bit. I guess using something like var tzoffset=dateengine[2].getTimezoneOffset() is better.

Thanks again for the help!

Just a little heads up, this Date Engine is actually part of a larger piece of code I'm working on, that will be able to find Date Related Information for the Client, and also ID the OS and Browser that the client is using, so please excuse any peculiarities. The reason why I include the version of the code is because I'm also working on a php code that will run daily, weekly, or monthly and will check whether the code it is using is the latest, and if not, update automatically.

Of course this code may ID things the client would otherwise not want (such as OS or Browser) so in the grand compilation, I will include codes that will allow the client to disable the Engine from detecting things such as Date, OS, or Browser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum