22

So, I'm trying to add some labels to a graph, and I want to add them to 6, 12, 18, and 24 hours on the horizontal axis.

I want to write these times in a "hh:mm" format (23:10, 05:10, 11:10, and 17:10 for example) for the local (computer) timezone?

Can someone help me with this?

Mario S
  • 11,715
  • 24
  • 39
  • 47
A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date – jbabey Oct 23 '12 at 15:38
  • If your problem is the formatting see my answer http://stackoverflow.com/questions/10645994/node-js-how-to-format-a-date-string-in-utc/10647272#10647272 – HBP Oct 23 '12 at 15:49

5 Answers5

41

based on How to add 30 minutes to a JavaScript Date object?

var d1 = new Date ();
var d2 = new Date ( d1 );
d2.setHours ( d1.getHours() + 6 );

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date will show how to manipulate Date objects.

added your code with some fixes. edited to add second document.write

<script type="text/javascript"> 
var timer = 24; 
var d1 = new Date(); 
var d2 = new Date();
d1.setHours(+d2.getHours()+(timer/4) ); 
d1.setMinutes(new Date().getMinutes()); 
document.write(d1.toTimeString("hh:mm"));
document.write(d1.getHours()+":"+d1.getMinutes());
</script>
Community
  • 1
  • 1
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
  • When I use this, I get an uncaught exception. Here's my code: – A_Elric Oct 23 '12 at 16:23
  • when i copied your code there was a strange character after the last semi-colon. also, where are you defining `now`? did you mean [Date.now()](http://msdn.microsoft.com/en-us/library/ff679974%28v=vs.94%29.aspx) because that returns the number of milliseconds since the epoch. – Pedro del Sol Oct 23 '12 at 16:29
  • As a final question, the output of that string is: 18:45:12 GMT-0400 (Eastern Daylight Time) how do I get rid of everything after the 18:45? – A_Elric Oct 23 '12 at 16:46
  • 2
    `document.write(d1.getHours()+":"+d1.getMinutes());` – Pedro del Sol Oct 23 '12 at 16:49
  • There's an error in your code. it should be var d1 = new Date (), d2 = new Date ( d1 ); You can't define a var after a comma. – Michel May 15 '16 at 09:30
9

try this

var today = new Date();
alert(today);
today.setHours(today.getHours()+6);
alert(today);
today.setHours(today.getHours()+6);
alert(today);
today.setHours(today.getHours()+6);
alert(today);
today.setHours(today.getHours()+6);
alert(today);
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
4
var MILLISECS_PER_HOUR = 60 /* min/hour */ * 60 /* sec/min */ * 1000 /* ms/s */;

function sixHoursLater(d) {
  return new Date(+d + 6*MILLISECS_PER_HOUR);
}

The numeric value of a date is milliseconds per epoch, so you can just add a number of milliseconds to it to get an updated numeric value.

The + prefix operator converts the date to its numeric value.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
2

Does this help?

function getDateString(addT){
    var time = new Date();
    time.setHours(time.getHours() + addT );
    return ((time.getHours()<10)?"0":"")+time.getHours() + ':' + time.getMinutes();
}

Then just use it pop out the data on the graph where you want?

ie:

for (i=0;i<=24;i+=6){
    yourbox.innerHTML = '<p>'+getDateString(i)+'</p>'; 
}

or somesuch;

Burkie
  • 76
  • 5
2

I like to do this like that

new Date(new Date().setHours(new Date().getHours() + 6))

or

new Date(new Date().setHours(new Date().getHours() + 6)).toString()