简体   繁体   中英

How to change a timezone to another timezone in netsuite while using restlet

I am sending lastmodified date via url in restlet but the problem is I have to modify the date based on user's timezone where restlet is deployed. SO, I need to change the timezone to his timezone before putting date in last modified filter. The problem I am facing is when I am using the new Date() function it is always changing the timezone to PST time zone instead of users time zone. Have a look at the question.

function getCompanyCurrentDateTime(UTCtime) { 
    var UTCtime = UTCtime; // Utc time is in milliseconds 
    var companyTimeZone = nlapiLoadConfiguration('companyinformation').getFieldText('timezone');
    nlapiLogExecution("DEBUG", "companyTimeZone",companyTimeZone );
    var timeZoneOffSet = (companyTimeZone.indexOf('(GMT)') == 0) ? 0 : new Number(companyTimeZone.substr(4, 6).replace(/\+|:00/gi, '').replace(/:30/gi, '.5'));
    nlapiLogExecution("DEBUG", "timeZoneOffSet",timeZoneOffSet ); // timezone offset is in hours and minutes 
    var timeZoneOffSetStr = timeZoneOffSet.toString();
    var timeZoneOffSetRes = timeZoneOffSetStr.split(".");
    var timeZoneOffSetMilli = Number(timeZoneOffSetRes[0])*3600*1000 + Number(timeZoneOffSetRes[1])*60*1000;
    nlapiLogExecution("DEBUG", "timeZoneOffSetMilli",timeZoneOffSetMilli );
    var NewcompanyDateTime = new Date(UTCtime + (timeZoneOffSetMilli));
    nlapiLogExecution("DEBUG", "NewcompanyDateTime",NewcompanyDateTime );
    return NewcompanyDateTime;
}

I use momentjs as library for timezone corrections than doing manual offset calculations.

You will need to include momentjs and its timezone data in your script as library

//I believe you already know code using nlapiLoadConfiguration on getting timezone and date format
moment(new Date()).tz(getComapnyTimeZone()).format(getCompanyDateFormat());
moment(new Date()).tz(getComapnyTimeZone()).format(getCompanyDateTimeFormat());

If the argument UTCtime is already universal standard, and you want to convert it to the browser's local time, you may want to try something like

function localTime(UTCtime)  // convert UTC in ms to local time in ms
{ return UTCtime + (new Date()).getTimezoneOffset() * 60 * 1000;
}

As noted in the question, getCompanyCurrentDateTime(UTCtime) will convert UTCtime to PST just as long as company time uses PST and that is the offset you apply to the parameter value!

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