簡體   English   中英

使用Restlet時如何在Netsuite中將時區更改為另一個時區

[英]How to change a timezone to another timezone in netsuite while using restlet

我正在通過restlet中的url發送最后修改日期,但問題是我必須根據部署restlet的用戶時區修改日期。 因此,在將日期放入上次修改的過濾器之前,我需要將時區更改為他的時區。 我面臨的問題是,當我使用新的Date()函數時,它總是將時區更改為PST時區,而不是用戶時區。 看看這個問題。

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;
}

我將momentjs用作時區校正的庫,而不是進行手動偏移量計算。

您需要在腳本中將momentjs及其時區數據包含在庫中

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

如果參數UTCtime已經是通用標准,並且您想將其轉換為瀏覽器的本地時間,則可能需要嘗試類似

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

正如問題所指出的,只要公司時間使用PST, getCompanyCurrentDateTime(UTCtime)會將UTCtime轉換為PST,這就是您應用於參數值的偏移量!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM