Date Example – Data Type – JavaScript Syntax – JS Date Example
Date Example – Data Type – JavaScript Syntax – JS Date Example
Purpose: – Illustrates the JavaScript syntax for the Date example.
Prerequistes:
- Install Visual Web Developer 2008
Syntax: var myCurrentDate = new Date(); – Demonstrates creating a Date object and various ways to instantiate, manipulate, and retrieve.
Restrictions: None
Notes:
- You can build your own library of syntax examples by using same web site over and over and just add new files to it.
Instructions:
- Use Visual Web Developer 2008
- Create new web site;
- Click File/New Web Site
- Select ASP.NET Website Template
- Select C-Sharp for Language
- name of Web Site could be JavaScript_Syntax.
- Add New folder named “DataType”
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: DataType
- Add HTML Page Named Date to DataType folder
- Right-click DataType folder;
- add new item;
- Select HTML Page
- HTML Page name could be Date
- Click on copy code in code below to copy code into HTML Page Date.htm
- Right-click on HTML page Date and select view in browser
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Boolean</title> <script type="text/javascript" > //***************************************************** // Four ways instantiate a Date object //***************************************************** // Note: Most of the parameters are optional and if you // omit them, a 0 is returned in its place. Months start // with Jan = 0; Feb = 1; etc //***************************************************** //1. new Date() // current date and time var myCurrentDate = new Date(); // current date and time document.write("myCurrentDate = " + myCurrentDate); document.write("<br />"); //2. new Date(milliseconds) //milliseconds since 1970/01/01 var myMillisecondsDate = new Date(50000000); //milliseconds since 1970/01/01 document.write("myMillisecondsDate = " + myMillisecondsDate); document.write("<br />"); //3. new Date(dateString) var myDateString = new Date("Jan 1, 2010"); document.write("myDateString = " + myDateString); document.write("<br />"); //4. new Date(year, month, day, hours, minutes, seconds, milliseconds) var myDateParts = new Date(2010, 1, 15, 13, 59, 48, 37); document.write("myDateParts = " + myDateParts); document.write("<br />"); //***************************************************** // SetDate method allows you to set a date or add or subtract to a date //***************************************************** var mySetDate = new Date(); mySetDate.setFullYear(2010, 0, 14); document.write("mySetDate = " + mySetDate); document.write("<br />"); var myGetDate10 = new Date(); myGetDate10.setDate(myGetDate10.getDate() + 10); document.write("myGetDate10 = " + myGetDate10); document.write("<br />"); //***************************************************** // How to format the date to locale area //***************************************************** var myDate = new Date(); document.write(myDate.toLocaleDateString()); document.write("<br />"); //***************************************************** // Date Object Methods //***************************************************** //Method Description //getDate() Returns the day of the month (from 1-31) //getDay() Returns the day of the week (from 0-6) //getFullYear() Returns the year (four digits) //getHours() Returns the hour (from 0-23) //getMilliseconds() Returns the milliseconds (from 0-999) //getMinutes() Returns the minutes (from 0-59) //getMonth() Returns the month (from 0-11) //getSeconds() Returns the seconds (from 0-59) //getTime() Returns the number of milliseconds since midnight Jan 1, 1970 //getTimezoneOffset() Returns the time difference between GMT and local time, in minutes //getUTCDate() Returns the day of the month, according to universal time (from 1-31) //getUTCDay() Returns the day of the week, according to universal time (from 0-6) //getUTCFullYear() Returns the year, according to universal time (four digits) //getUTCHours() Returns the hour, according to universal time (from 0-23) //getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999) //getUTCMinutes() Returns the minutes, according to universal time (from 0-59) //getUTCMonth() Returns the month, according to universal time (from 0-11) //getUTCSeconds() Returns the seconds, according to universal time (from 0-59) //getYear() Deprecated. Use the getFullYear() method instead //parse() Parses a date string and returns the number of milliseconds since midnight of January 1, 1970 //setDate() Sets the day of the month (from 1-31) //setFullYear() Sets the year (four digits) //setHours() Sets the hour (from 0-23) //setMilliseconds() Sets the milliseconds (from 0-999) //setMinutes() Set the minutes (from 0-59) //setMonth() Sets the month (from 0-11) //setSeconds() Sets the seconds (from 0-59) //setTime() Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 //setUTCDate() Sets the day of the month, according to universal time (from 1-31) //setUTCFullYear() Sets the year, according to universal time (four digits) //setUTCHours() Sets the hour, according to universal time (from 0-23) //setUTCMilliseconds() Sets the milliseconds, according to universal time (from 0-999) //setUTCMinutes() Set the minutes, according to universal time (from 0-59) //setUTCMonth() Sets the month, according to universal time (from 0-11) //setUTCSeconds() Set the seconds, according to universal time (from 0-59) //setYear() Deprecated. Use the setFullYear() method instead //toDateString() Converts the date portion of a Date object into a readable string //toGMTString() Deprecated. Use the toUTCString() method instead //toLocaleDateString() Returns the date portion of a Date object as a string, using locale conventions //toLocaleTimeString() Returns the time portion of a Date object as a string, using locale conventions //toLocaleString() Converts a Date object to a string, using locale conventions //toString() Converts a Date object to a string //toTimeString() Converts the time portion of a Date object to a string //toUTCString() Converts a Date object to a string, according to universal time //UTC() Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time //valueOf() Returns the primitive value of a Date object //***************************************************** // How to Compare a date //***************************************************** var myDate = new Date(); myDate.setFullYear(2010, 06, 14); var today = new Date(); if (myDate > today) { alert("Today is before 14th July 2010"); } else { alert("Today is after 14th July 2010"); } </script> </head> <body> </body> </html> |