Languages
[Edit]
EN

JavaScript - get current time

7 points
Created by:
Savannah
559

In this article, we would like to show you how to get the current time using Date class in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

var now = Date.now();

console.log('Current unix millisecond time: ' + now);

1. Predefined methods example

// ONLINE-RUNNER:browser;

var now = new Date();

console.log('Current unix millisecond time: ' + now.getTime());
console.log('Current date: ' + now.toDateString());
console.log('Current time: ' + now.toTimeString());
console.log('Current date & time: ' + now.toString());

Note: during using to...String methods it is necessaryto be careful because of specific formatting of data and time on the locale.

3. Custom formatting

JavaScript API provides different methods that let us create our own date and time formatting.

3.1. Modern JavaScript (>= ES6) custom date & time formatting example

renderNumber method can be replaced with built-in padStart method when modern JavaScript API is used.

// ONLINE-RUNNER:browser;

const renderNumber = (value, length) =>
{
    let result = String(value);

    for (; length > result.length; length -= 1)
        result = '0' + result;

    return result;
};

const renderDate = (date) =>
{
    const year = renderNumber(date.getFullYear(), 4);
    const month = renderNumber(date.getMonth() + 1, 2);
    const day = renderNumber(date.getDate(), 2);

    return `${year}.${month}.${day}`;
};

const renderTime = (date) =>
 {
    const hours = renderNumber(date.getHours(), 2);
    const minutes = renderNumber(date.getMinutes(), 2);
    const seconds = renderNumber(date.getSeconds(), 2);
    const milliseconds = renderNumber(date.getMilliseconds(), 3);

    return `${hours}:${minutes}:${seconds}.${milliseconds}`;
};

const renderDateTime = (date) =>
{
    const dateText = renderDate(date);
    const timeText = renderTime(date);
  
    return `${dateText} ${timeText}`;
};

// Usage example:

var now = new Date();

var date = renderDate(now);
var time = renderTime(now);
var dateTime = renderDateTime(now);

console.log('Current date: ' + date);
console.log('Current time: ' + time);
console.log('Current date & time: ' + dateTime);

3.1. Old JavaScript custom date & time formatting example

// ONLINE-RUNNER:browser;

function renderNumber(value, length) {
    var result = String(value);
    for(; length > result.length; length -= 1) {
        result = '0' + result;
    }
    return result;
}

function renderDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var text = renderNumber(year, 4) 
        + '.' + renderNumber(month, 2) 
        + '.' + renderNumber(day, 2);
    return text;
}

function renderTime(date) {
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    var millisecond = date.getMilliseconds();

    var text = renderNumber(hour, 2) 
        + ':' + renderNumber(minute, 2) 
        + ':' + renderNumber(second, 2) 
        + '.' + renderNumber(millisecond, 3);
    return text;
}

function renderDateTime(date) {
    var dateText = renderDate(date);
    var timeText = renderTime(date);
  
    return dateText + ' ' + timeText;
}

// Usage example:

var now = new Date();

var date = renderDate(now);
var time = renderTime(now);
var dateTime = renderDateTime(now);

console.log('Current date: ' + date);
console.log('Current time: ' + time);
console.log('Current date & time: ' + dateTime);

References

  1. Date class - MDC Docs

Alternative titles

  1. How to get current time in JavaScript?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join