 |
<!--This example uses the Date object to determine the day from an entered date.--> <HTML> <HEAD> <TITLE> Date Example </TITLE> <SCRIPT LANGUAGE="JavaScript"> function whichDayWasThat(date) { // Validate the input if (!/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/.test(date)) { alert('Date format should be as follows:\nD-M-YYYY or DD-MM-YYYY.\n\nPlease try again'); return; } // Define the vars and objects this.date = date.split('-'); days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); myDate = new Date(); // Do some date stuff myDate.setMonth(parseInt(this.date[1]-1),this.date[0]); // Januari = 0, so substract 1 from the input myDate.setFullYear(this.date[2]); // Alert the result alert(this.date.join('-') + ' was/is a ' + days[myDate.getDay()]); } </SCRIPT> </HEAD> <BODY> <form name=f> Enter a date. Example: 18-12-1966<br> <input type="text" size="40" name="d"><br> <input type="button" value="Get the day" onClick="whichDayWasThat(document.f.d.value)"> </form> </BODY> </HTML>
|
|