/    Sign up×
Community /Pin to ProfileBookmark

How to minus 5 minutes from current time?

Hey guys,

I’m trying to subtract 5 minutes from the current time and print it. Here is what I have so far:

[CODE]<script type=”text/javascript”>
var a_p = “”;
var d = new Date();
var curr_hour = d.getHours();
if (curr_hour < 12)
{
a_p = “AM”;
}
else
{
a_p = “PM”;
}
if (curr_hour == 0)
{
curr_hour = 12;
}
if (curr_hour > 12)
{
curr_hour = curr_hour – 12;
}

var curr_min = d.getMinutes();

curr_min = curr_min + “”;

if (curr_min.length == 1)
{
curr_min = “0” + curr_min;
}

document.write(curr_hour + “:” + curr_min + ” ” + a_p);

</script>[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 18.2009 — You'd better use the [B]setMinutes()[/B] method:
<i>
</i>var date=new Date();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
date.setMinutes(minutes-5,seconds,0);
var HH=date.getHours();
var MM=date.getMinutes();
HH&lt;10?HH='0'+HH:null;
MM&lt;10?MM='0'+MM:null;
alert(HH+':'+MM);

The precision is no more than around +- 1 second. If you want a higher precision (which I doubt), you may use the milliseconds as well
<i>
</i>var date=new Date();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
var milliseconds=date.getMilliseconds();
date.setMinutes(minutes-5,seconds,milliseconds);
var HH=date.getHours();
var MM=date.getMinutes();
HH&lt;10?HH='0'+HH:null;
MM&lt;10?MM='0'+MM:null;
alert(HH+':'+MM);
Copy linkTweet thisAlerts:
@runningthingzauthorOct 18.2009 — Thanks for that. How do I add that into my existing code? Sorry, I'm just starting out with JS.
Copy linkTweet thisAlerts:
@astupidnameOct 19.2009 — Actually, in this case I wouldn't use setMinutes(), but use setTime() instead and subtract 300,000 milliseconds from getTime(). You could incorporate it into your current script with just one line added in:
&lt;script type="text/javascript"&gt;
var a_p = "";
var d = new Date();
[COLOR=red]d.setTime(d.getTime() - 300000); [/COLOR][COLOR=darkorchid]//60,000 ms per minute * 5 = 300,000 ms[/COLOR]
var curr_hour = d.getHours();
if (curr_hour &lt; 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
if (curr_hour == 0)
{
curr_hour = 12;
}
if (curr_hour &gt; 12)
{
curr_hour = curr_hour - 12;
}
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if (curr_min.length == 1)
{
curr_min = "0" + curr_min;
}
document.write(curr_hour + ":" + curr_min + " " + a_p);
&lt;/script&gt;

And actually, if you wanted to tighten up the script a bit, it really boils down to the following:
&lt;script type="text/javascript"&gt;
var d = new Date();
d.setTime(d.getTime() - 300000); //60,000 ms per minute * 5 = 300,000 ms
var curr_hour = d.getHours();
var a_p = (curr_hour &lt; 12) ? " A.M." : " P.M.";
curr_hour = (curr_hour &gt; 12) ? curr_hour - 12 : (curr_hour === 0) ? 12 : curr_hour;
var curr_min = d.getMinutes();
curr_min = (curr_min &lt; 10) ? "0" + curr_min : curr_min;
document.write(curr_hour + ":" + curr_min + a_p);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@justinbarneskinOct 19.2009 — the date object converted to string displays different with the browser, but it looks like military time-

[code=html]<script type="text/javascript">
var d = new Date()
document.write(d+'<hr>')
var now=d.getTime();
var then=Number(d.getTime()-300000);
d.setTime(now); var n=d;n=n.toString();
if(navigator.appName=='Microsoft Internet Explorer'){document.write('<BIG><B>now</B></BIG> '+ n.substring(10,19)+'<br>')}
else{document.write('<i>now</i> '+ n.substring(16,24)+'<br>')}
d.setTime(then); var t=d;t=t.toString();
if(navigator.appName=='Microsoft Internet Explorer'){document.write('<BIG><B>then</B></BIG> '+ t.substring(10,19)+'<br>')}
else{document.write('<i>then</i> '+ n.substring(16,24)+'<br>')}
</script>[/code]
Copy linkTweet thisAlerts:
@KorOct 19.2009 — Thanks for that. How do I add that into my existing code? Sorry, I'm just starting out with JS.[/QUOTE]
You don't incorporate. You simply use that code.
Copy linkTweet thisAlerts:
@saruqueNov 14.2018 — var date1 = new Date();

date1.setMinutes(date1.getMinutes()-10);

document.write(date1);

// See the reference: https://www.codespeedy.com/subtract-minutes-from-date-in-javascript/
×

Success!

Help @runningthingz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...