Free Web Development Online Tutorials, Learn to Code
JavaScript Date Function, JavaScript Date Time AM PM example, difference between two dates in Javascript

JavaScript Date Function Example

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.

JavaScript Date Methods

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();

Javascript getDate function

To get current date in javascript we need to use new Date(), this date object will return the current date.

var d = new Date();
document.write("Date is" + d.getDate());<br />
// date result here

Javascript getMonth method

The javascript getMonth() method returns number (0-11) as the month of a date, so you may consider number + 1

var d = new Date();
document.write("Month is" + d.getMonth());<br />
// month result here

Javascript getFullYear method

Use Javascript getFullYear() method to get the year, not getYear()

var d = new Date();
document.write("Year is" + d.getFullYear());<br />
// year result here

Capture Current Time in JavaScript Example

To get the time in javascript you can concatinate getHours() getMinutes() getSeconds()

  • Javascript getHours() method
    var _date = new Date();
    document.writeln("Hours:"+ _date.getHours());
  • Javascript getMinutes() method
    var _date = new Date();
    document.writeln("Minutes:"+ _date.getMinutes());
  • Javascript getSeconds() method
    var _date = new Date();
    document.writeln("Seconds:"+ _date.getSeconds());
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>");            

// Current time result here

Find Date Time AM PM in JavaScript

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.

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()));

Get date difference in JavaScript

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.

JavaScript Date Time Examples

javascript Interview Questions Answers

Get Time AM PM in JavaScript
JavaScript Examples | JavaScript Online Course