return to top of page

Javascript Work Area

The Function ShowNameAge resides in the <head> section of the HTML code but may be called anywhere in the <body> of the web page and may be called multiple times throughout the page (as demonstarted below with Jim repeated). This Javascript routine was created in 2016. Name and Age are hard coded for the current year but the function should adjust the age for any year in the future and it calculates the birth year for each entry. This could be set up to prompt for Name and Age and display the results but I am trying hard to understand Functions and won't complicate at this time but may return to modify it later.

There is NO attempt on this page to determine whether it is EST or DST at this location currently. Examples indicate the difference between EST and DST.
Fairly complex logic could determine whether it is EST or DST currently at this location but I passed on writing the code.

In reality the code as written here here does not reflect DST time accurately between midnignt and 1 A.M. diplaying the hour as -1 (11 P.M. the previous day). Although not perfect I don't intend to write extensive calculations to change the date/time (which could be year, month, and hour on certain dates).

DST_Error
The date and time are represented incorrectly as mentioned above.

If DST remains in use, and there is serious argument against it world wide, the times here will always assume EST, therefore DST will be off by an hour from actual DST. There are valid argumenst for making DST permanent, moving our clocks ahead by an hour, to save energy by extending the hours of daylight during human activity. Time will tell how we resolve this controversial issue.
Some of this may seem simple, and it is, but learning for me is by baby steps and repetition in many cases.




From time to time I find (after exhaustive search) code that perfoms really useful functions and incorporate it here. I try to understand and in many cases modify the code to do more or to do things I consider of value for the result or the learning experience.



musical notes musical notes


Jim Croce – Time In A Bottle  –  Lyrics





Formatted Intervals Demo

Date: YYYY-MM-DD Date: YYYY-MM-DD




Display Day of Week for Any Date

Enter a date (use the same format as the example):


example 26 December, 1977
    

Day of week appears HERE


 


Return to Top of Page

Using Date Elements

SAMPLE ONLY Not the actual date for today! See next section with actual code.
The simplest javascript to get Current Year

var FullDateToday = new Date();
Result: FullDateToday = Sat Nov 26 2016 15:20:59 GMT-0500 (Eastern Standard Time)

Calculated next year two different ways (produces same result as you would expect)
Followed by built in javascript functions demonstrating how to get various elements that make up the complete and very detailed date construct. The building blocks of the Date can be further manipulated to get desired results i.e. Day and Month names (and much more as will be shown).

NOTE: The variable FullDateToday is user defined and could be anything you consider meaningful to you. There are javascript reserved variables that you can NOT use to represent something else than what it is designed to represent.


javascript begins here

– output from locally imbeded javascript code.

Week number, frequently used in industry, can also be perplexing, see the examples.

2014/12/29 is Monday in week 1 of 2015 and
2012/01/01 is Sunday in week 52 of 2011

Standars are important, to make sure we are all talking about the same thing, otherwise major confusion can cause irreperable damage or costly errors.

Week 47 is from Monday November 21, 2016 until (and including) Sunday November 27, 2016.

ISO week, according to the ISO-8601 standard, starts with Monday: It is the first week with a majority (4 or more) of its days in January. The first week of the year is the week that contains that year's first Thursday (='First 4-day week').

– Output from inc/WeekNumber.js external javascript.


Same source page. Hope results are always identical. ISO weeknumber is very complex. Locally imbedded code here.


Table of Some Date Elements

 

Method Function What the Function Does Output
getFullYear() Returns the 4 digit year: e.g. 2017
getUTCFullYear() Returns the 4 digit year: e.g. 2017
getMonth() Returns the number of months into the year: (0-11)   0 = January, 11 = December
getUTCMonth() Returns the number of months into the year:
getDate() Returns the current day of month: (1-31)
getUTCDate() Returns the current day of month: (1-31)
getDay() Returns the current day of the week: (0-6)   0=Sunday, 1=Monday ... 6=Saturday
getUTCDay() Returns the current day of the week: (0-6)
getHours() Returns the current number of hours into the day: (0-23)
getUTCHours() Universal Time Coordinated / Universal Coordinated Time, Current hours (0-23)
getMinutes() Returns the current number of minutes into the hour: (0-59)
getSeconds() Returns the current number of seconds into the minute: (0-59)
getTime() Number of milliseconds since 1 January 1970
getTimezoneOffset() Returns minutes, between UTC and local time

NOTE:
Single digit Months, Days, Hours, Minutes or Seconds are often prepadded with 0 (zero) e.g. 01, 09. Providing a uniform appearance to displayed dates and time.

NOTE:
UTC Universal Time Coordinated / Universal Coordinated Time is Successor to: Greenwich Mean Time (GMT). Since time (Hour) of day determines when the day changes from one day to the next, it stands to reason that not only the day can be different in two locations but the date, month and even the year can change at those critical times of the calendar year. We have all watched the world celebrate the New Year long before it arrives at our location or long after we have celebrated the event locally.

Displaying elements in the format you prefer. See the confusion that can result, especially for single digit months and single digit days.
Note some of the various ways the date is displayed and of course there are many more.
Is 3/6/2000 March the sixth or June the third?
To avoid confusion due to the order of the elements being displayed, the ISO standard was adopted for international use.

Listing of javascript Date Elements

 


Return to Top of Page

Manipulating Date Elements

This excersise started off as a simple learning experience. I hard coded my birthday
birthDate = new Date("March 6, 1943");
This may seem wonky to the uninitiated but it is legitimate javascript code. From this simple argument I started manipulating the information, with additional coding, to produce various results. To confirm that each new piece of code is working as I wanted it to, I output the result; in most cases the variable name to verify and examine the code and logic. In many cases each new idea lead to another new idea that is a useful result, and a challange to anticipate every possibility that could exist. Perhaps the major next calculation will be how many days or hours old you are. Currently the precison is only to the month level of the current year but this will be addressed soon to allow calculations crossing into the next or previous year (i.e. Birthday was ? months ago or will be ? months from now; when these evets occour in the previous or next year).

I may eventually allow user input to calculate the same information for any user (once I progress far enough). As you can see this is a work in progress.



Hard coded birth date of 1943,02,06 ( YYYY,MM,DD = 1943 March 6)
NOTE: Months Jan = 0 Feb = 1 Mar = 2

The Actual Code Begins Here

 



Rounding Fractions with Javascript
floor round or ceil

Demonstrate the difference between the fraction rounding of values.

Tiny = 25.0001
Small = 25.49
Large = 25.51

var Math.floor(value); var Math.round(value); var Math.ceil(value);

floor always rounds fractions Down to the lower integer value.
round rounds fractions to the nearest integer value Up or Down.
ceil always rounds fractions Up to the Higher integer value.

Athough I used round (Rounding to the nearest integer) in AGE calculations, perhaps it would be more advantageous or logical to use ceil to round UP, since the lower valued age has already been passed. I am NOT planning to change the calculation in PhotoAlbum.html but you never know; I might just do it one day.

 



 



Return to Top of Page

 


Convert Date Difference to Years Months Days

 



Day of Week and Month Names

Using arrays two different ways

This section is javascript code followed by results. Note that // represents a comment for programmer to clarify the code for easier understanding later or by another programmer. Commenting code is very useful in case you need to debug the code later.

Array for determining the name of the current Day. Sunday is always day 0 but
if you have a need you can write code to change that to suit your specific needs.

var dayName = new Array();
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] = "Tuesday";
dayName[3] = "Wednesday";
dayName[4] = "Thursday";
dayName[5] = "Friday";
dayName[6] = "Saturday";


Same results from the following. Added the Month Names as well.

Coding for Day Names:

var TheDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

ThisDayName = TheDays[ThisDay];

Month Names:

var TheMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

ThisMonthName = TheMonths[ThisMonth-1]; // -1 to compensate for January being month 0

document.write("ThisMonthName = <b>" + ThisMonthName + "</b><br>");
document.write("ThisDayName = <b>" + ThisDayName + "</b><br>");

Result:

 


Simple Date Time Elements

Note that Pre Padding single digit elements makes the result more uniform and easier to read.

I must admit I am not a huge fan of the way ISO Date & Time are displayed
— Date followed by T followed by the time e.g. 2018-03-06T06:55:00 with no space between the date and the T or between the T and the time. In reality I see no need for the T since the time elements are separated by : (colons). I much prefer a space or space @ space. I have examples of how I would prefer to see it but I don't set standards, I merely follow them (grudgingly sometimes but). Adding time difference -500 or time zone EST for clarification is a value add to the information being conveyed , however ideally it should be a specific number of characters i.e. UTC EST TOT TUT for the same reason that single digits get prepadded. Currently time zones can have 1 to 5 cnaracters i.e. A, U, W, Z, TAHT, ROTT, ULAST; but I guess it is asking too much to have total uniformity. Here again the time difference seems most logical and easiest to understand.

The Difference Between GMT and UTC

• GMT is a time zone officially used in some European and African countries. The time can be displayed using both the 24-hour format (0 - 24) or the 12-hour format (1 - 12 A.M./P.M.).
Greenwich Mean Time On 1 November 1884, GMT was adopted universally at the International Meridian Conference in Washington, DC, USA. As a result, the International Date Line was drawn up and 24 time zones were created.

• UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide. This means that no country or territory officially uses UTC as a local time. In 1960, the International Radio Consultative Committee formalized the concept of UTC, and it was put into practice the year after. The name Coordinated Universal Time was officially adopted in 1967.
UTC was adjusted several times until 1972, when leap seconds were introduced to keep UTC in line with the Earth's rotation, which is not entirely even, and less exact than atomic clocks.

Why UTC for Coordinated Universal Time? UTC does not favour any particular language. In addition, the advantage of choosing UTC is that it is consistent with the abbreviation for Universal Time. Language neutrality sounds political to me but the world is happy with the decision.

 



Return to Top of Page

Days Left This Year

This code returns nothing but the number of days remaining in current year as  

 

An elegant and simple script to calculate the number of days remaining in the current year. This code can be used to calculate the days remaining from a given date to any future date (or days since a given date).



Added inc/daysremaining.js, inc/daysremaining1.js and inc/DaysLeftThisYear.js to eliminate the need to repeat the code everywhere you wish to use it.
Added inc/DaysLeftThisYear.js which produces the message today is the final day of
Added inc/days2xmas1.js produces the number of days to Christmas and inc/days2xmas.js which produces the number of days remaining till Christmas with approriate text.

inc/daysremaining1.js =

inc/daysremaining.js = (0 days remaining this year)

inc/DaysLeftThisYear.js = (today is the final day of )

inc/days2xmas1.js = (-1 ... -5)

inc/days2xmas.js = (Christmas was 1 ... 5 days ago NOT -3 before)


Combined

JavaScript output

DayMonthDate.js
daycount.js
days2xmas1.js
days2xmas.js   OR   Christmas was X days ago! (instead of -X before)
daysremaining.js
DaysLeftThisYear.js  OR  today is the final day of (instead of 0)

on many webpages with additional appropriate text as useful information. See Below.


Today is: , and

Return to Top of Page

Age Calculator

Select The Day Month Year of Your Birth


Month   Date    Year     




You have been living for:
In months:
In days:
In hours:
In minutes:
Your next birthday will be in:

Source of Inspiration – Customized but this is the source of the code.


Return to Top of Page

Find Date of Events DaySearch.js

Displays the date of events such as Family Day Third Monday of February, Canadian Thanksgiving on the Second Monday of October or US Thanksgiving Fourth Thursday of November. Here we are using the external imbed of inc/DaySearch.js whereas above the code is included in the HTML. Nice to learn how to take advantage of code across multiple web pages without bloating the page with repetitious code.

Family Day in is on February


Great use of these tools is displayed in the following Perpatual table

Canadian Holidays – Current and Next year

 


Return to Top of Page

Display Date Difference in Years Months Days

Using Local Code External version date2date.js folows; only a prompt is required to make this really great.

If I could creat a prompt for user input and process the output with this I would be very HAPPY!
The output is Exactly what I am looking for.
I could modify the code to display the day of birth your Next Birthday including what day it will be etc...

Code Source – Also inc/date2date external to eliminate need to repeat code

 


Using External Script

date2date.js


I made great use of this on the PhotoAlbum page Some Interesting Statistics – Automatically keeps the information current. Followed up by the Never Ending Story with a lot of personal dates that remain current without any modifications or updates.

 


Date Functions

The following is output from javascript.
Note that Month Date Hour Minute and Second may be single digits. In each case where a single digit item is displayed the same item is displayed prepadded with a zero, e.g. 8 becomes 08.
I like the esthatics of uniform 2 digit output.

 


Javascript Tests

 


Return to Top of Page


Great Validation Get Birth Date

This program may look simple at first glance but it has very accurate and sophisticated verification code right down to the day level. What is lacking in the information provided (age in years today) is well compensated for by precision.
I am not a big fan of popup messages or answers but this gem has great date validation code with meaningful and much needed popup error messages. I hope I can adapt this code to other date type functions. I modified the popup from a simple number representing your age with a little more meanigful message. I modified the message indicating you are 18 or older, adding a message for under 18. I added logic and message for those of retirement age over 65.

Code Source – Nice to share useful code.

 

Enter Birth Date: (in MM/DD/YYYY format)

There is lot of code and it only display your age in full years period. Good exercise but not too useful.

 




Calculating the Number of Days in a Year

365d 5h 48m 56.89s for the March equinox. [or 365.242325 ephemeris days]
365d 5h 47m 58.13s for the June solstice. [or 365.241645 ephemeris days]
365d 5h 48m 38.81s for the September equinox. [365.242116 ephemeris days]
365d 5h 49m 35.21s for the December solstice. [365.242769 ephemeris days]
365d 5h 48m 47.26s for the average of four. [or 365.242214 epehmeris days]

The average length of the Vernal Equinox year is currently 365.24238 days (to the mearest one-hundred-thousandth of a day). The Dee value will continue to be the most reliabe measure of the length of an Erth year.

Not as simple as it may seem since we use the rotation of our planet to calculate our time. Who knew it could be so complicated a topic for something we seem to take for granted.




Add Commas to Numbers

JavaScript has no built in function to add commas to large numbers. Commas make large numbers easier to read 2,645,936 is much easier to read than 2645936. Added a JavaSCript file AddComma.js which allows commas to be added to larger numbers for ease of reading. Applied the code below.

AddCommas.js



Requires AddSuffix.js in Head Section

Add superscript suffix to any number eg. 1st 2nd 3rd etc...   (variable.AddSuffix())  (mixed case)





CDN Celebrations | New Years | Valentine's Day | St. Patrick's Day | Easter | Mother's Day | Father's Day | Canada Day | Thanksgiving | Halloween | Remembrance Day | Christmas


 

Perpetual Table of Canadian Holidays

 

 


Today is: , and !

 

maple-leaf bar divider

Silver Box at End of Driveway e-mail:   jimandgloria@yahoo.com

You Light Up My Life
As the World Turns





Return to Top of Page

Timelapse ISS Assembly Video

Video Tour Inside the ISS




Thank you for visiting us on the World Wide Web.
Copyright © 2000 - All rights reserved.
Design by Φ Unicorn JIF 2007 2008
All suggestions, Broken Links, Corrections and Recommendations must be submitted to WebMaster

   Send e-mail to:   WebMaster jimandgloria@yahoo.com


Unicorn - Your Web Master Santa Jim - Your Web Master Unicorn - Your Web Master