0

I know that

var currentTime = new Date();
var currentOffset = currentTime.toISOString();

will give current date & time in IST format. Can anyone help me how to get past 2 hours date & time in IST format

Alexa
  • 77
  • 11
  • Possible duplicate of [Adding hours to Javascript Date object?](https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) – Spencer Wieczorek Jun 05 '18 at 02:57

1 Answers1

0

To calculate a time difference, you can use a combination of the relevant get and set methods. After you get the value, you perform the desired calculation and use the result as the argument for the set.

Note that the default timezone is based on system settings. So performing such a change has no bearing on the timezone (i.e. for me the code outputs in PDT).

var time = new Date();
var currentOffset = time.getTimezoneOffset();

console.log('Current time: ' + time.toISOString());
console.log('Current offset: ' + currentOffset);

time.setHours(time.getHours() - 2);
var pastOffset = time.getTimezoneOffset();

console.log('Past time: ' + time.toISOString());
console.log('Past offset: ' + currentOffset);
chuckx
  • 6,484
  • 1
  • 22
  • 23
  • But I need time stamp in ISO Format – Alexa Jun 05 '18 at 05:29
  • Then you just use [Date.prototype.toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString). Updated example in answer. – chuckx Jun 05 '18 at 05:32