If there’s a good way depends. Fact is, there is no built-in method for that. But you can build one on your own by using Date.getTimezoneOffset()
and doing some modulus. Here’s a lead:
// set up date 2009-02-13T23:31:30.123Z (equivalent to 1234567890123 milliseconds):
var localDate = new Date(1234567890123);
// get local time offset, like -120 minutes for CEST (UTC+02:00):
var offsetUTC = new Date().getTimezoneOffset();
// set date to local time:
localDate.setMinutes(localDate.getMinutes() - offsetUTC);
offsetUTC = {
// positive sign unless offset is at least -00:30 minutes:
"s": offsetUTC < 30 ? '+' : '-',
// local time offset in unsigned hours:
"h": Math.floor(Math.abs(offsetUTC) / 60),
// local time offset minutes in unsigned integers:
"m": ~~Math.abs(offsetUTC) % 60
};
offsetUTC = offsetUTC.s + // explicit offset sign
// unsigned hours in HH, dividing colon:
('0'+Math.abs(offsetUTC.h)+':').slice(-3) +
// minutes are represented as either 00 or 30:
('0'+(offsetUTC.m < 30 ? 0 : 30)).slice(-2);
localDate = localDate.toISOString().replace('Z',offsetUTC);
// === "2009-02-13T23:31:30.123+02:00" (if your timezone is CEST)
Or a little less verbose:
localDate = localDate.toISOString().replace('Z',(offsetUTC<30?'+':'-')+
('0'+Math.floor(Math.abs(offsetUTC)/60)+':').slice(-3)+
('0'+((~~Math.abs(offsetUTC)%60)<30?0:30)).slice(-2));
Note that…