CrazyNooB
CrazyNooB

Reputation: 1593

How to set current time to some other time in javascript

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.

Upvotes: 7

Views: 17322

Answers (5)

Hugo G
Hugo G

Reputation: 16496

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.

Upvotes: 6

Geeky Guy
Geeky Guy

Reputation: 9399

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 now.getDay() would return the day of the week, not the day of the month. Hence the now.getDate() up there. (Edited to use a base date instead of a fixed, hard-coded one).

Upvotes: 7

123
123

Reputation: 525

Try:

<script>
function startTime()
{
var today=new Date(Thu Aug 08 2013 13:34:55 GMT+0200);
var h=today.setHours();
var m=today.setMinutes();
var s=today.setSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}

function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>

<body onload="startTime()">
<div id="txt"></div>
</body>

Upvotes: 0

123
123

Reputation: 525

You can try:

The long way:

var date = new Date();
var now = date.getTime();
now.setTime( 0,0,0,0 );
now.setMonth(4);
now.setDate(5);
now.setYear(2013);

The short way:

var date = new Date("Sat Apr 05 2013 19:30:13 GMT+0200 (IDT)");

Upvotes: -2

JohnnyJS
JohnnyJS

Reputation: 1472

Simple:

var x = new Date("Fri Aug 09 2013 19:30:13 GMT+0300 (IDT)");

Now the date of x is two days ahead (09/08/2013)

Upvotes: -2

Related Questions