简体   繁体   中英

Past 2hours date and Time in IST Format javascript

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

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 (ie 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM