www.webdeveloper.com
Results 1 to 5 of 5

Thread: please help with [birth.getDay()]) question

  1. #1
    Join Date
    Nov 2014
    Posts
    11

    Unhappy please help with [birth.getDay()]) question

    var MONTHS='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC';
    var dayArray = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    var dob=prompt('Enter your Birthdate [DD MMM YYYY]','01 JAN 2000')
    var dateSplit=dob.split(' ')
    var day=dateSplit[0];
    var month=dateSplit[1];
    var year=dateSplit[2];
    var m=(MONTHS.indexOf(month.toUpperCase())/3);


    document.writeln('<li><strong>I was born on a '+(dayArray[birth.getDay()])+': </strong><em><font color="blue">'

    I'dLike to know how the very last line of this code works.
    When splitting the date into an array the birth.getday() could be anywhere between 1 and 31 could it not ?
    yet the dayArray() has only 7 elements sunday to saturday ?
    so how is it putting out one of those days ? very confused ! Help please ?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    The getDay() function looks at the date (1-31) to determine if it falls on a Sunday (0) to Saturday (6).
    The getDay() function does this automatically (a native JS command) when a valid date has been analyzed.
    See: http://www.w3schools.com/jsref/jsref_getday.asp for more information.

    BTW, you should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier for the forum members to read it. It also retains any formatting you use in your programs.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    Without any error checking of the user's input, you could simplify the script a bit...
    Code:
    <script type="text/javascript">
    var MONTHS=['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
    var dayArray = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var dob=prompt('Enter your Birthdate [DD MMM YYYY]','01 JAN 2000')
    
    var birth = new Date(dob);  // alert(birth.toString());  // for testing purposes only
    
    document.writeln('<strong>I was born on a '+dayArray[birth.getDay()]+' </strong>');
    document.writeln('<em style="color:red"> in the Month of '+MONTHS[birth.getMonth()]+'. of '+birth.getFullYear()+'</em>');
    
    </script>
    However, I would recommend you added some checks in case the user cannot follow instructions.

  4. #4
    Join Date
    Nov 2014
    Posts
    11

    Thanks so much that is so helpful , Im going through your code to see where i went wr

    IM going throught your code to see where i went wrong. Java scrit vaiables and the writeln method are hard to get right as i am beginning !

    Code:
    var MONTHS='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC';
    var dayArray = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    var dob=prompt('Enter your Birthdate [DD MMM YYYY]','01 JAN 2000')
    var dateSplit=dob.split(' ')
    var day=dateSplit[0];
    var month=dateSplit[1];
    var year=dateSplit[2];
    var m=(MONTHS.indexOf(month.toUpperCase())/3);
    
    
    document.writeln('<li><strong>I was born on a '+(dayArray[birth.getDay()])+': </strong><em><font color="blue">'

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    To add a bit about date calculation, consider the following
    HTML Code:
    <!DOCTYPE html >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    
    <script>
    // calculate a day of week version 1
    Date.prototype.getDayOfWeek =function(){
    	return ['Sun','Mon','Tues','Wednes','Thurs','Fri','Satur'][this.getDay()] + "day";
    }
    
    // calculate a day of week version 2
    DayOfWeek = {
    	'Sun':'',
    	'Mon':'',
    	'Tue':'s',
    	'Wed':'nes',
    	'Thu':'rs',
    	'Fri':'',
    	'Sat':'ur',
    	getDOW:function(d){
    		if(d){
    			d = d.toUTCString().slice(0,3); // we only need first 3 characters
    			for(day in DayOfWeek){
    				if(typeof DayOfWeek[day]!='undefined' || typeof DayOfWeek[day]!='function'){
    					if( day==d ) return day+DayOfWeek[day]+"day";
    				}
    			}
    		
    		}
    	return "Unknown day : "+d;
    	}
    }
    
    // The original input method
    var getDOB = prompt('Enter your Birthdate [DD MMM YYYY]','01 JAN 2000');
    
    // Pass it to the Date object, see if it spits out a usable date ref
    dob = new Date( getDOB );
    
    // output sone feedback on the date according to the prompt input
    console.log(">> This is what Date() has determined as the actual date -> "+dob );
    
    // use method 1 to work out DOW
    console.log(">> You were born on a -> '"+dob.getDayOfWeek() +"' as determined by the 'Date.prototype.getDayOfWeek' method." );
    
    // use method 2 to calculate DOW
    console.log(">> You were born on a -> '"+DayOfWeek.getDOW( dob )+"' as determined by the 'DayOfWeek.getDOW' method" );
    
    // function used in web form date picker (HTML5) if not 
    // supported by browser, the input field will be a regular text field
    function calc(o){
    	console.log("> " + new Date(o.chosendate.value).getDayOfWeek() );
    return false;
    }
    </script>
    </head>
    <body>
    <form name="datepick" action="javascript:;" onsubmit="return calc(this);">
      <input type="date" name="chosendate" value="" />	
      <input type="submit" name="Submit" value="Submit" />
    </form>
    </body>
    </html>
    The Date object, contrary to some beliefs is actually very good at determining dates by passing a date reference in the various methods at your disposal.

    I find that the most reliable method is passing an RFC formatted date which is in the format of DD MMM YYYY but the date object also works well with the date picker format "YYYY-MM-DD" and more reliable method is to use millisesonds, however, you will see many ways of chopping up a date, using the individual methods for date creation . extraction when 99.999% of the time, all that you need is to pass the string or numeric to Date and pull out what you need from a toUTCString method or a getTime method.

    IMHO the biggest problem is the education system which is about 25 years behind in its teaching of things like JavaScript.

    You will need to hit the F12 key to open your browsers console to see output like
    Code:
     >> This is what Date() has determined as the actual date -> Sat Jan 01 2000 00:00:00 GMT+0000 (GMT Standard Time)
    >> You were born on a -> 'Saturday' as determined by the 'getDayOfWeek' method.
     >> You were born on a -> 'Saturday' as determined by the 'DayOfWeek.getDOW' method
    > Saturday
    Last edited by \\.\; 12-06-2014 at 06:46 AM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles