How to set current time to some other time in javascript -
i trying set current time other time , whenever try access current time need new time. suppose current time 2 in local machine. want change 10 , current time should keep ticking 10 instead of 2 whenever access it.
ex:
var x = new date();
if see x.gethours() fetches local time 2 , every time keeps ticking 2 base.
but need change 10 new date() gives 10 , keeps ticking every second based on that.
i not want use setinterval() purpose. sure have solution setinterval(). thing have multiple setintervals , other 1 stopping updating time in setinterval() trying update 10 every second.
you can't change system time javascript, not if it's running browser. you'd need special environment, 1 ability interface javascript , operating system's api that. that's no simple thing , way out of scope of application you're working on.
i suggest create function/object means fetch current date offset. this:
foo = function () {}; foo.prototype.offset = 8; foo.prototype.getdate = function () { var date = new date(); date.sethours(date.gethours() + this.offset); return date; }
now can instantiate foo
, set offset (8 default) , work it. when need offset hour can do:
var foo = new foo(); var bar = foo.getdate();
bar
not tick, whenever need current date offset may use foo
's getdate
again.
edit: in order start fixed date, can use constructor this:
foo = function (basedate) { this._date = basedate; this._fetched = new date(); } foo.prototype.getdate = function () { var = new date(); var offset = now.gettime() - this._fetched.gettime(); this._date.settime(this._date.gettime() + offset); this._fetched = now; return this._date; }
notice (edited use base date instead of fixed, hard-coded one).now.getday()
return day of week, not day of month. hence now.getdate()
there.
Comments
Post a Comment