We can use Date () object to manipulate dates in java script. Whenever we want to access current date of the system or want to set new date in java script then we can use appropriate java script method of date function. In this demonstration we learn how to work with function of date object.
Some important function related with date object
- Date () -Date () function return current system date with time.
- toDateString() - toDateString() function return current date without time.
- getDay() - getDay() method returns current day of weak.
- getDate() - getDate() method returns current day of month in numeric.
- getMonth() -getMonth() method returns current month.
- getFullYear() - getFullYear() method return full year.
- setDate() - setDate() method is used to set new date of month.
- setMonth () - setMonth() method is used to set new month.
- setFullYear() - setFullYear() method is used to set new full year.
Following example demonstrate use of Date object in java script
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function dateObject() {
//Displaying current system date and time.
document.write("Current System Date With Time :" + Date() + "<br />");
//Displaying only current system date only.
var currentDate = new Date();
document.write("Current System Date Without Time : " + currentDate.toDateString() + "<br />");
//Displaying Current System Day.
document.write("Current System Day : " + currentDate.getDay() + "<br />");
//Displaying current numeric day
document.write("Current System Numeric Day : " + currentDate.getDate() + "<br />");
//Display Current Month.
document.write("Current System Month : " + currentDate.getMonth() + "<br />");
//Displaying Full Year
document.write("Current Full Year : " + currentDate.getFullYear() + "<br />");
/*
We can also set new date also or new month by addong set prefix in all
the methods as like setDate(), setMonth() etc..
*/
//Setting new Date.
currentDate.setDate(25);
//Setting new month.
currentDate.setMonth(8);
//Setting new year.
currentDate.setFullYear(1989);
//Viewing new date.
document.write("New System Date : " + currentDate.toDateString());
}
</script>
</head>
<body>
<input type="button" value="Click this to view message" onclick="dateObject()" />
</body>
</html>
Anonymous User
02-May-2019Thanks for sharing the valuable information.