GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Date
Selected Reading
JavaScript - DateThe Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown below. Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object using either local time or UTC (universal, or GMT) time. The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent the date and time till the year 275755. SyntaxYou can use any of the following syntaxes to create a Date object using the Date() constructor − new Date( ) new Date(milliseconds) new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ]) Note − Parameters in the brackets are always optional. Parameters
Return ValueIt returns the date string containing day, month, date, year, hours, minutes, seconds, and timezone, as shown below. Wed Aug 09 2023 09:24:03 GMT+0530 (India Standard Time) JavaScript Date ReferenceIn JavaScript, the Date object provides methods for creating, manipulating, and formatting dates and times. Here, we have listed all the methods present in Date class − JavaScript Date MethodsHere is a list of the methods used with Date and their description. Date Static MethodsThese methods are invoked using the Date object −
Date MethodsThese methods are invoked using the instance of the Date object −
Date Object ConstructorFollowing is the Date Object Constructor in JavaScript −
Example: Creating JavaScript Date ObjectIn the example below, we create a new instance of the date object. In the output, you can observe that it returns the current time. <html>
<head>
<title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date();
document.getElementById("output").innerHTML =
"Today's date is : " + date;
</script>
</body>
</html>
OutputIf we execute the above program, it returns the current time. Example: Setting up custom dateIn the example below, We have passed the custom date string as a parameter of the Date() constructor to create a custom date. The Date() constructor returns the standard date string, which you can see in the output. <html>
<head>
<title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date("August 19, 2024 09:30:54");
document.getElementById("output").innerHTML =
"The custom date is : " + date;
</script>
</body>
</html>
OutputIf we execute the above program, it returns the custom time as provided. ExampleIn the example below, we have passed milliseconds as an argument of the Date() constructor. If you pass the positive milliseconds as an argument, the object returns the date according to the 1st January 1970 00:00:00 + milliseconds. Otherwise, it returns the date according to the 1st January 1970 00:00:00 milliseconds if negative milliseconds passed as an argument. <html>
<head>
<title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
let date = new Date(999999999999);
output.innerHTML += "The Date after 1st January, 1970 is - " + date + "<br>";
date = new Date(-999999999999);
output.innerHTML += "The Date before 1st January, 1970 is - " + date;
</script>
</body>
</html>
OutputIt returns the date after 1st January, 1970 and before 1st January, 1970 as result. Example: Constructing a date from 7 argumentsIn the example below, we have passed the year, month, date, hour, minute, second, and millisecond as a Date() constructor argument. The Date() constructor returns the full date string, which you can see in the output. <html>
<head>
<title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date(2001, 5, 14, 6, 43, 58, 342);
document.getElementById("output").innerHTML =
"The custom date is : " + date;
</script>
</body>
</html>
OutputIf we execute the above program, it returns the custom time as provided. However, you can use the different methods of the Date object to format the date string. Lets look at the example below. Example: Formatting a date stringIn the example below, three different methods are used to format the date string. The toDateString() method extracts the date only from the date string and removes the time part. The toISOString() method converts the date string into the ISO format. The toUTCString() method converts the date string into the UTC time format. <html>
<head>
<title> JavaScript - Formatting the date </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date(999999999999);
document.getElementById("output").innerHTML +=
"The Date after 1st January, 1970 is: " + date.toDateString() + "<br>"+
"The Date after 1st January, 1970 is: " + date.toISOString() + "<br>"+
"The Date after 1st January, 1970 is: " + date.toUTCString();
</script>
</body>
</html>
OutputIt will return the ouptut of the above provided methods, respectively. Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |