How to set current time to some other time in javascript
Solution 1
You can't change the system time from Javascript, not if it's running from the browser. You'd need a special environment, one with the ability to interface javascript and the operating system's API to do that. That's no simple thing and probably way out of the scope of the application you're working on.
I suggest you create a function/object with means to fetch the current date with an offset. Like this:
Foo = function () {};
Foo.prototype.offset = 8;
Foo.prototype.getDate = function () {
var date = new Date();
date.setHours(date.getHours() + this.offset);
return date;
}
Now you can instantiate a foo
, set the offset (8 by default) and work with it. When you need your offset hour you can just do:
var foo = new Foo();
var bar = foo.getDate();
bar
will not tick, but whenever you need the current date with an offset you may just use Foo
's getDate
again.
Edit: In order to start from a fixed date, you can use the constructor like this:
Foo = function (baseDate) {
this._date = baseDate;
this._fetched = new Date();
}
Foo.prototype.getDate = function () {
var now = new Date();
var offset = now.getTime() - this._fetched.getTime();
this._date.setTime(this._date.getTime() + offset);
this._fetched = now;
return this._date;
}
Notice that (Edited to use a base date instead of a fixed, hard-coded one).now.getDay()
would return the day of the week, not the day of the month. Hence the now.getDate()
up there.
Solution 2
Using TimeShift.js this can be done fairly easy. in the beginning of your page include TimeShift.js and set the date explicitly. Shameless copy of its manual:
new Date().toString(); // Original Date object
"Fri Aug 09 2013 23:37:42 GMT+0300 (EEST)"
Date = TimeShift.Date; // Overwrite Date object
new Date().toString();
"Fri Aug 09 2013 23:37:43 GMT+0300"
TimeShift.setTimezoneOffset(-60); // Set timezone to GMT+0100 (note the sign)
new Date().toString();
"Fri Aug 09 2013 21:37:44 GMT+0100"
TimeShift.setTime(1328230923000); // Set the time to 2012-02-03 01:02:03 GMT
new Date().toString();
"Fri Feb 03 2012 02:02:03 GMT+0100"
TimeShift.setTimezoneOffset(0); // Set timezone to GMT
new Date().toString();
"Fri Feb 03 2012 01:02:03 GMT"
TimeShift.getTime(); // Get overridden values
1328230923000
TimeShift.getTimezoneOffset();
0
TimeShift.setTime(undefined); // Reset to current time
new Date().toString();
"Fri Aug 09 2013 20:37:45 GMT"
new Date().desc(); // Helper method
"utc=Fri, 09 Aug 2013 20:37:46 GMT local=Fri, 09 Aug 2013 20:37:46 GMT offset=0"
new TimeShift.OriginalDate().toString(); // Use original Date object
"Fri Aug 09 2013 23:37:47 GMT+0300 (EEST)"
I think this is slightly better than writing a custom Date object/function (Foo) since this library doesn't require you to rewrite existing code.
CrazyNooB
I am the greatest noob ever lived :D(my english is not correct too)
Updated on July 16, 2022Comments
-
CrazyNooB almost 2 years
I am trying to set current time to some other time and whenever i try to access current time i need to get the new time. Suppose current time is 2 am in my local machine. But I want to change it to 10 am and current time should keep ticking from 10 am instead of 2 am whenever i access it.
Ex:
var x = new Date();
if you see x.getHours() fetches my local time which is 2 am and every time it keeps ticking with 2 am as its base.
But I need to change it to 10 am so that my new Date() gives 10 am and keeps ticking every second based on that.
I do not want to use setInterval() for this purpose. I sure have solution with setInterval(). But the thing is I have multiple setIntervals and the other one is stopping from updating the time in the setInterval() where I am trying to update 10 am every second.
-
Geeky Guy over 10 yearsOnly problem is that
x
doesn't update its time every tick like the OP needs. Do read the question again. -
CrazyNooB over 10 yearsI am checking your solution Renan. Will reply soon.
-
Geeky Guy over 10 yearsWhat the OP needs is a date variable that self-updates every tick. Your
d
doesn't do that. Also notice thatn
is a number and doesn't have theDate
type methods. Try that code in your browser's console. -
Geeky Guy over 10 yearsThere, I fixed it with a functional version.
-
Bergi over 10 years
Date.prototype.addHours=function(h){this.setHours(this.getHours()+h);return this};
:-) -
Geeky Guy over 10 years@Bergi thanks :) ended up without modifying Date's prototype, though.
-
CrazyNooB over 10 years@Renan it sure is working. One more thing, I want it to tick with 10 am right from beginning, not taking into my local minutes and seconds into consideration. I mean it should keep ticking right from the first second of 10 AM. any thoughts on that..?
-
Geeky Guy over 10 yearsI am going to edit my answer to include that, one moment please.
-
Geeky Guy over 10 yearsThere. I have written a version that starts from a fixed date.
-
CrazyNooB over 10 years@Renan how about i need something dynamic, I mean instead of 10 am it can be a user defined one...?
-
Geeky Guy over 10 yearsThen instead of setting a hard coded 10, receive the desired date in the constructor. Another edition to my answer.
-
CrazyNooB over 10 years@Renan instead of this._date it should be this.date, i believe, because it is throwin me error.
-
CrazyNooB over 10 years@Renan can we have that base date in integer, like if it is 1 pm it will be 13, so it wont be a date actually, but an integer.
-
Geeky Guy over 10 yearsThis is going way beyond the scope of the original question - but if you can catch the idea of what I'm doing in my answer, you can adjust it to receive a number instead of a date, and then you can use that to make your own base date ;)
-
CrazyNooB over 10 years@Renan right i am changing it. Thanks for your prompt response. I am working on it.
-
CrazyNooB over 10 years@Renan worked like a charm. You made my day. Thanks for your quick and, to the point reply :)