When we write any program we often consider date as parameter, here we learn how to use date in JavaScript programming.
In JavaScript we have Date object, which provides many built-in properties and methods to find different format of date and time.
There are many built-in function in javascript, which gets date, day, time, hours, minute, seconds etc.
for example to get date in Javascript, you need to write new Date();
To get current date in javascript we need to use new Date()
, this date object will return the current date.
<script> var d = new Date(); document.write("Date is" + d.getDate());<br /> </script>// date result here
The javascript getMonth()
method returns number (0-11) as the month of a date,
so you may consider number + 1
<script> var d = new Date(); document.write("Month is" + d.getMonth());<br /> </script>// month result here
Use Javascript getFullYear()
method to get the year, not getYear()
<script> var d = new Date(); document.write("Year is" + d.getFullYear());<br /> </script>// year result here
To get the time in javascript you can concatinate getHours() getMinutes() getSeconds()
var _date = new Date(); document.writeln("Hours:"+ _date.getHours());
var _date = new Date(); document.writeln("Minutes:"+ _date.getMinutes());
var _date = new Date(); document.writeln("Seconds:"+ _date.getSeconds());
<script> var d = new Date(); document.write("Hour is " + d.getHours() +"</br>"); document.write("Minute is " + d.getMinutes() + "</br>"); document.write("Second is " + d.getSeconds() + "</br>"); </script>
// Current time result here
Now you may have a question how to know if the time is AM or PM right? You have to use some logic as given below.
<script> function getAMPM(date) { var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); var ampm = hours >= 12 ? 'PM' : 'AM'; hours = hours % 12; hours = hours ? hours : 12; // if the hour is '0' then should be '12' minutes = minutes < 10 ? '0' + minutes : minutes; var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm; return strTime; } document.write("Current Time is " + getAMPM(new Date())); </script>
Here we see how to find the difference between two dates in javascript.
Normally we consider difference in day, means how many days we have between two dates, but in JavaScript we can also calculate time difference between two dates. Let’s see some example.
const date1 = new Date('10/10/2016'); const date2 = new Date('10/10/2020'); const diffTime = Math.abs(date2 - date1); console.log("difference in milliseconds:" + diffTime);
As you can see in above code, by default we get time difference in milliseconds.
Now we need to calculate how many days we have in that number of milliseconds.
/* 24 hours 60 Minutes 60 Seconds 1 second = 1000 milliseconds */ const dayMilliseconds = (1000 * 60 * 60 * 24); const dayDiff = Math.ceil(diffTime / dayMilliseconds); console.log(dayDiff + " days");
The above calculation will get you the exact day difference between two dates in JavaScript.