...

View Full Version : Javascript Date Engine Ver2.0



Omega9850
05-17-2012, 06:58 AM
Once again, I've made an update in the Javascript Date Engine. For the most part, it is similar to Ver1.1 but uses a different method in calculating date objects. I've also added a Leap Year Detector.
I've made 3 versions
Custom: Includes all comments in an easy to read format.
Standard Inflated: Similar to Custom, with all comments removed.
Standard Deflated: Similar to Standard Inflated, with all unnecessary spaces and lines removed.


Custom

//DATE ENGINE
var deversion="2.0 Build 5.11.12"; //Date Engine Version
var deedition="Custom"; //Date Engine Edition ("Custom" or "Standard")

var fdate=new Date(); //Full Date (Fri May 11 2012 00:00:00 GMT-0700 (US Mountain Standard Time))
var mhours=fdate.getHours(); //Military Hours (0-23)
var minutes=fdate.getMinutes(); //Minutes (0-59)
var seconds=fdate.getSeconds(); //Seconds (0-59)

if (fdate.getTimezoneOffset() === 0)
{
sessionStorage.setItem("TimeZone","UTC+0:00");
}
if (fdate.getTimezoneOffset() > 0)
{
if ((fdate.getTimezoneOffset()/60).toString().charAt(1) === "" && (fdate.getTimezoneOffset()/60).toString().charAt(2) === "")
{
sessionStorage.setItem("TimeZone","UTC-" + (fdate.getTimezoneOffset()/60) + ":00" );
}
if ((fdate.getTimezoneOffset()/60).toString().charAt(1) == "." && (fdate.getTimezoneOffset()/60).toString().charAt(2) > 0 && (fdate.getTimezoneOffset()/60).toString().charAt(3) === "")
{
sessionStorage.setItem("TimeZone","UTC-" + (fdate.getTimezoneOffset()/60).toString().charAt(0) + ":" + (fdate.getTimezoneOffset()/60).toString().charAt(2)*0.6 + "0");
}
if ((fdate.getTimezoneOffset()/60).toString().charAt(1) == "." && (fdate.getTimezoneOffset()/60).toString().charAt(2) > 0 && (fdate.getTimezoneOffset()/60).toString().charAt(3) > 0)
{
sessionStorage.setItem("TimeZone","UTC-" + (fdate.getTimezoneOffset()/60).toString().charAt(0) + ":" + ((fdate.getTimezoneOffset()/60).toString().charAt(2) + (fdate.getTimezoneOffset()/60).toString().charAt(3))*0.6);
}
}
if (fdate.getTimezoneOffset() < 0)
{
if (Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(1) === "" && Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(2) === "")
{
sessionStorage.setItem("TimeZone","UTC+" + Math.abs(fdate.getTimezoneOffset()/60) + ":00" );
}
if (Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(1) == "." && Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(2) > 0 && Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(3) === "")
{
sessionStorage.setItem("TimeZone","UTC+" + Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(0) + ":" + Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(2)*0.6 + "0");
}
if (Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(1) == "." && Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(2) > 0 && Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(3) > 0)
{
sessionStorage.setItem("TimeZone","UTC+" + Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(0) + ":" + (Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(2) + Math.abs(fdate.getTimezoneOffset()/60).toString().charAt(3))*0.6);
}
}

if (mhours === 0)
{
sessionStorage.setItem("meridiem","AM");
sessionStorage.setItem("hour",mhours + 12);
//Any Meridiem Dependent Variables Here
}
if (mhours >= 1 && mhours <= 11)
{
sessionStorage.setItem("meridiem","AM");
sessionStorage.setItem("hour",mhours);
//Any Meridiem Dependent Variables Here
}
if (mhours == 12)
{
sessionStorage.setItem("meridiem","PM");
sessionStorage.setItem("hour",mhours);
//Any Meridiem Dependent Variables Here
}
if (mhours >= 13 && mhours <= 23)
{
sessionStorage.setItem("meridiem","PM");
sessionStorage.setItem("hour",mhours - 12);
//Any Meridiem Dependent Variables Here
}

switch(mhours)
{
case 0:
//Any Hour Dependent Variables Here
break;
case 1:
//Any Hour Dependent Variables Here
break;
case 2:
//Any Hour Dependent Variables Here
break;
case 3:
//Any Hour Dependent Variables Here
break;
case 4:
//Any Hour Dependent Variables Here
break;
case 5:
//Any Hour Dependent Variables Here
break;
case 6:
//Any Hour Dependent Variables Here
break;
case 7:
//Any Hour Dependent Variables Here
break;
case 8:
//Any Hour Dependent Variables Here
break;
case 9:
//Any Hour Dependent Variables Here
break;
case 10:
//Any Hour Dependent Variables Here
break;
case 11:
//Any Hour Dependent Variables Here
break;
case 12:
//Any Hour Dependent Variables Here
break;
case 13:
//Any Hour Dependent Variables Here
break;
case 14:
//Any Hour Dependent Variables Here
break;
case 15:
//Any Hour Dependent Variables Here
break;
case 16:
//Any Hour Dependent Variables Here
break;
case 17:
//Any Hour Dependent Variables Here
break;
case 18:
//Any Hour Dependent Variables Here
break;
case 19:
//Any Hour Dependent Variables Here
break;
case 20:
//Any Hour Dependent Variables Here
break;
case 21:
//Any Hour Dependent Variables Here
break;
case 22:
//Any Hour Dependent Variables Here
break;
case 23:
//Any Hour Dependent Variables Here
break;
}

var d=fdate.getDate(); //Days (1-31)
var dnamedarray=new Array(7);
dnamedarray[0]="Sunday";
dnamedarray[1]="Monday";
dnamedarray[2]="Tuesday";
dnamedarray[3]="Wednesday";
dnamedarray[4]="Thursday";
dnamedarray[5]="Friday";
dnamedarray[6]="Saturday";
var dnamed=dnamedarray[fdate.getDay()]; //Days (Saturday-Sunday)

switch(dnamed)
{
case "Sunday":
sessionStorage.setItem("Yesterday","Saturday");
sessionStorage.setItem("Tomorrow","Monday");
//Any Day Dependent Variables Here
break;
case "Monday":
sessionStorage.setItem("Yesterday","Sunday");
sessionStorage.setItem("Tomorrow","Tuesday");
//Any Day Dependent Variables Here
break;
case "Tuesday":
sessionStorage.setItem("Yesterday","Monday");
sessionStorage.setItem("Tomorrow","Wednesday");
//Any Day Dependent Variables Here
break;
case "Wednesday":
sessionStorage.setItem("Yesterday","Tuesday");
sessionStorage.setItem("Tomorrow","Thursday");
//Any Day Dependent Variables Here
break;
case "Thursday":
sessionStorage.setItem("Yesterday","Wednesday");
sessionStorage.setItem("Tomorrow","Friday");
//Any Day Dependent Variables Here
break;
case "Friday":
sessionStorage.setItem("Yesterday","Thursday");
sessionStorage.setItem("Tomorrow","Saturday");
//Any Day Dependent Variables Here
break;
case "Saturday":
sessionStorage.setItem("Yesterday","Friday");
sessionStorage.setItem("Tomorrow","Sunday");
//Any Day Dependent Variables Here
break;
}

var m=fdate.getMonth()+1; //Months (1-12)
var mnamedarray=new Array(12);
mnamedarray[0]="January";
mnamedarray[1]="February";
mnamedarray[2]="March";
mnamedarray[3]="April";
mnamedarray[4]="May";
mnamedarray[5]="June";
mnamedarray[6]="July";
mnamedarray[7]="August";
mnamedarray[8]="September";
mnamedarray[9]="October";
mnamedarray[10]="November";
mnamedarray[11]="December";
var mnamed=mnamedarray[m-1]; //Months (January-December)

switch(mnamed)
{
case "January":
sessionStorage.setItem("LastMonth","December");
sessionStorage.setItem("NextMonth","February");
//Any Month Dependent Variables Here
break;
case "February":
sessionStorage.setItem("LastMonth","January");
sessionStorage.setItem("NextMonth","March");
//Any Month Dependent Variables Here
break;
case "March":
sessionStorage.setItem("LastMonth","February");
sessionStorage.setItem("NextMonth","April");
//Any Month Dependent Variables Here
break;
case "April":
sessionStorage.setItem("LastMonth","March");
sessionStorage.setItem("NextMonth","May");
//Any Month Dependent Variables Here
break;
case "May":
sessionStorage.setItem("LastMonth","April");
sessionStorage.setItem("NextMonth","June");
//Any Month Dependent Variables Here
break;
case "June":
sessionStorage.setItem("LastMonth","May");
sessionStorage.setItem("NextMonth","July");
//Any Month Dependent Variables Here
break;
case "July":
sessionStorage.setItem("LastMonth","June");
sessionStorage.setItem("NextMonth","August");
//Any Month Dependent Variables Here
break;
case "August":
sessionStorage.setItem("LastMonth","July");
sessionStorage.setItem("NextMonth","September");
//Any Month Dependent Variables Here
break;
case "September":
sessionStorage.setItem("LastMonth","August");
sessionStorage.setItem("NextMonth","October");
//Any Month Dependent Variables Here
break;
case "October":
sessionStorage.setItem("LastMonth","September");
sessionStorage.setItem("NextMonth","November");
//Any Month Dependent Variables Here
break;
case "November":
sessionStorage.setItem("LastMonth","October");
sessionStorage.setItem("NextMonth","December");
//Any Month Dependent Variables Here
break;
case "December":
sessionStorage.setItem("LastMonth","November");
sessionStorage.setItem("NextMonth","January");
//Any Month Dependent Variables Here
break;
}

var lyear=fdate.getFullYear()-1; //Last Year (2011)
var year=fdate.getFullYear(); //Year (2012)
var nyear=fdate.getFullYear()+1; //Next Year (2013)

if (lyear%4 === 0)
{
sessionStorage.setItem("LYearLeapYear","Yes");
sessionStorage.setItem("LYearModulus4","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("LYearLeapYear","No");
sessionStorage.setItem("LYearModulus4","Fail");
//Any Leap Year Dependent Variables Here
}
if (lyear%400 === 0)
{
sessionStorage.setItem("LYearLeapYear","Yes");
sessionStorage.setItem("LYearModulus400","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("LYearModulus400","Fail");
if (sessionStorage.getItem("LYearModulus4") == "Pass")
{
sessionStorage.setItem("LYearLeapYear","Yes");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("LYearLeapYear","No");
//Any Leap Year Dependent Variables Here
}
}
if (year%4 === 0)
{
sessionStorage.setItem("YearLeapYear","Yes");
sessionStorage.setItem("YearModulus4","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("YearLeapYear","No");
sessionStorage.setItem("YearModulus4","Fail");
//Any Leap Year Dependent Variables Here
}
if (year%400 === 0)
{
sessionStorage.setItem("YearLeapYear","Yes");
sessionStorage.setItem("YearModulus400","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("YearModulus400","Fail");
if (sessionStorage.getItem("YearModulus4") == "Pass")
{
sessionStorage.setItem("YearLeapYear","Yes");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("YearLeapYear","No");
//Any Leap Year Dependent Variables Here
}
}
if (nyear%4 === 0)
{
sessionStorage.setItem("NYearLeapNYear","Yes");
sessionStorage.setItem("NYearModulus4","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("NYearLeapNYear","No");
sessionStorage.setItem("NYearModulus4","Fail");
//Any Leap Year Dependent Variables Here
}
if (nyear%400 === 0)
{
sessionStorage.setItem("NYearLeapNYear","Yes");
sessionStorage.setItem("NYearModulus400","Pass");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("NYearModulus400","Fail");
if (sessionStorage.getItem("NYearModulus4") == "Pass")
{
sessionStorage.setItem("NYearLeapNYear","Yes");
//Any Leap Year Dependent Variables Here
}
else
{
sessionStorage.setItem("NYearLeapNYear","No");
//Any Leap Year Dependent Variables Here
}
}

Just like the previous versions, it seems to work on all major browsers, and Android.

NOTE: I couldn't add the Standard Versions because the post would be too long, so I've attached them both in a zip file attached to this post.

felgall
05-17-2012, 11:43 AM
Simple leap year detector


leapyear = function(year) {
"use strict";
var dt = new Date(year, 1, 29);
return 29 === dt.getDate();
}

if (leapyear(2012)) {...

If it isn't a leap year setting that date will result in the getDate returning 1 instead of 29.

Peeyush
05-19-2012, 12:06 PM
What can it do? i mean Javascript Date engine Ver 2.0?

Omega9850
05-24-2012, 09:05 AM
Sorry, I should probably include that in the posts. The Javascript Date Engine (JsDE) allows a website to have access to the time (Year, Month, Day, Hour, Minute, Second) in both a code friendly (numbers) and user friendly (words) format. It can also determine the TimeZone, tell which Days, Months, and Years are next, and can tell whether a year is a leap year or not.

All of this is achieved through Javascript alone, so no server side scripting is needed. It doesn't really have much of a practical use, except if you want your website to change depending on the time. I just thought it would be cool.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum