SQL PHP HTML ASP JavaScript articles and free scripts to download
 

setHours()

sethours() can fix the hours part of date function of JavaScript. We can set the hours from 0 to 23. This can only work on date object. Here is the sample code.

var dt= new Date();
document.write(dt + "<br><br>");
dt.setHours(2);
document.write(dt);

The output is here

We can set the hours value to previous value also by using negative value . Just change the this line in above code.

dt.setHours(-2);

The output is here

We can set the hours value to beyond 23 hours also. Here is one example.

dt.setHours(25);

Output is here

We have seen by using sethours we can fix the hours part to any value. This value is independent of present hours value. Try to display the date and time with hours value to display time after two hours from present value ( after reading the present hour ) .
To do this we have to read the present hours part and then add two hours to it. For this new value of hours we have to use sethours function the date object.


Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
Date & Time