Skip to content Skip to sidebar Skip to footer

Current Time (javascript) In Html

Hi currently I had a javascript that display current time. Is a example taken from the internet. How do I go about doing it such that the current time displayed is 5mins behind the

Solution 1:

Try:

var nowMinusFiveMinutes = newDate(Date.now() - (5 * 60 * 1000));

Note:

  1. Calling Date.now() gets the number of milliseconds since the epoch (Jan 1, 1970).
  2. (5 * 60 * 1000) is the number of milliseconds in 5 minutes.
  3. You can construct a date by using new Date(numberOfMillisecondsSinceTheEpoch).

UPDATE:

I don't see the need for three separate setInterval() calls. Couldn't you update the three elements in one call, like this?

Solution 2:

try to minus no of minutes that you want to...

setInterval( function() {
// Create a newDate() object and extract the minutes of the current time on the visitor'svar minutes = newDate().getMinutes();
minutes = minutes - 5; // here you can minus or add minutes as per your requirements// Add a leading zero to the minutes value
$("#min").html(( minutes < 10 ? "0" : "" ) + minutes);
},1000);

UPDATED

var currDateTime = newDate(),
    hr = currDateTime.getHours(),
    min = currDateTime.getMinutes(),
    dd = currDateTime.getDate(),
    mm = currDateTime.getMonth() + 1,
    yyyy = currDateTime.getFullYear();

//adding pad while getting singles in date or month between 0-9if(dd < 10){ dd = '0' + dd}     if(mm < 10){ mm = '0' + mm} 

//current date and time as per UTC format
currDateTime = yyyy +'/'+ mm +'/'+ dd +" "+ hr + ':' + min + ":00";
$("#currDateTime").text(newDate(currDateTime).toUTCString());

//minus -5 minutes to actual date and time
currDateTime = yyyy +'/'+ mm +'/'+ dd + " "+ hr + ':' + (min - 5) + ":00";
$("#minusDateTime").text(newDate(currDateTime).toUTCString());

//we are now going to add +5 min to currDateTime
currDateTime = yyyy +'/'+ mm +'/'+ dd + " "+ hr + ':' + (min + 5)+ ":00";
$("#addDateTime").text(newDate(currDateTime).toUTCString());

This way you can add or minus no of hours, minutes, days, months, years to specific objects as per requirements..

Let me know for further clarifications?

Hope it would helps! Thanks

Here is a JSFiddle demo

Solution 3:

You can convert the date to time in milliseconds, and subtract 5 minutes worth of milliseconds (1 min = 60k milliseconds = 5 minutes/300k milliseconds).

var x = newDate;
x = x.valueOf();
x -= 300000;

And then convert that however you wish.

Post a Comment for "Current Time (javascript) In Html"