我想从JavaScript获取客户端时区id以解析c#TimezoneInfo类。并转换为utc time.And我有这个
var timezone = String(new Date());
return timezone.substring(timezone.lastIndexOf('(') + 1).replace(')', '').trim();
问题是javascript timezone返回CST会有一段时间。是否有正确的方法来获取时区ID
来自c#
TimeZoneInfo ZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneIdFromJavascript);
return TimeZoneInfo.ConvertTimeToUtc(Datetime, ZoneInfo);'
答案 0 :(得分:9)
TimeZoneInfo
使用 Windows 时区标识符。它们不会匹配任何来自JavaScript的东西。请阅读the timezone tag wiki。TimeZoneInfo
做得好得多。答案 1 :(得分:3)
我找到的最安全的方法是仅使用UTC的偏移量,而不使用标识符名称。
我使用Javascript发送此邮件:
//@version=4
//Created by ChrisMoody on October 23, 2014 by user request - belgusinc
//Changes Barcolor when Stochastic is Overbought Oversold.
//Necessary for Highlight Bars
//Only Necessary if you want you want Stochastic Croses
study(title="CM_Stochastic_Highlight Bars", shorttitle="CM_Stoch_HighlightBars v4", overlay=true)
len = input(14, minval=1, title="Length for Stochastic")
smoothK = input(3, minval=1, title="SmoothK for Stochastic")
smoothD = input(3, minval=1, title="SmoothD for Stochastic")
upLine = input(80, minval=50, maxval=90, title="Upper Line Value?")
lowLine = input(20, minval=10, maxval=50, title="Lower Line Value?")
//Not Necessary, In Inputs Tab, capability to turn On/Off Highlight Bars, and Both Crosses
//These are all checkboxes in inputs tab....Show Barcolor Highlights, Show Background Hghlight, Plot B and S for Buy Sell
sbc = input(true, title="Show Price Bar highlight When Stoch is Above/Below High/Low Lines?")
sbh = input(false, title="Show Background highlight When Stoch is Above/Below High/Low Lines?")
sch = input(false, title="Show Back Ground Highlights When Stoch Cross - Strict Criteria - K Greater/LesThan High/Low Line - Crosses D ?")
sl = input(true, title="Show 'B' and 'S' Letters When Stoch Crosses High/Low Line & D?")
sac = input(false, title="Show Back Ground Highlights When Stoch Cross - Any Cross?")
sacl = input(false, title="Show 'B' and 'S' Letters When Stoch Crosses - Any Cross?")
//Necessary for Highlight Bars
//Stoch formula
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
//Necessary for Highlight Bars
//aboveline = OverBought, belowline = Oversold Definitions
aboveLine = k > upLine ? 1 : 0
belowLine = k < lowLine ? 1 : 0
//Only Necessary if you want you want Stochastic Croses
//Definition for Cross when Above High-Low lines
crossUp = (k[1] < d[1] and k[1] < lowLine[1]) and (k > d) ? 1 : 0
crossDn = (k[1] > d[1] and k[1] > upLine[1]) and (k < d) ? 1 : 0
//Only Necessary if you want you want Stochastic Croses
//Definition for Cross that doesn't have to be above or below High and Low line.
crossUpAll = (k[1] < d[1] and k > d) ? 1 : 0
crossDownAll = (k[1] > d[1] and k < d) ? 1 : 0
var lastCrossUpPrice = 0.
var lastCrossDnPrice = 10e10
crossUpAlert = false
crossDnAlert = false
if crossUpAll and not crossUpAll[1]
crossUpAlert := close > lastCrossUpPrice
lastCrossUpPrice := close
if crossDownAll and not crossDownAll[1]
crossDnAlert := close < lastCrossDnPrice
lastCrossDnPrice := close
//Only Necessary if you want background Highlighting also - Take out the sbh/sbc and part
//BackGround Highlights based on Above/Below Lines, Strict Cross Up/Down
//BackGroound Color Plots
bgcolor(sbh and aboveLine ? color.red : na, transp=70)
bgcolor(sbh and belowLine ? color.lime : na, transp=70)
bgcolor(sch and crossUp ? color.lime : na, transp=40)
bgcolor(sch and crossDn ? color.red : na, transp=40)
//Only Necessary if you want background Highlighting also
//plots bgcolor Cross with no filter
bgcolor(sac and crossUpAll ? color.lime : na, transp=40)
bgcolor(sac and crossDownAll ? color.red : na, transp=40)
//Necessary for Highlight Bars
//Highlight Bar Definitions
overBought() => sbc and aboveLine
overSold() => sbc and belowLine
//Necessary for Highlight Bars
//Highlight Bar Plot Statement based on Above Definitions
barcolor(overBought() ? color.orange : overSold() ? color.fuchsia : na)
//Not Necessary if you just want Highlight Bars - Extra Feature
//These are just the Letters B and Sell Based on Crosses
plotchar(sl and crossUp ? crossUp : na, title="Buy Signal Strict Criteria", char='B', location=location.belowbar, color=color.lime, transp=0, offset=0)
plotchar(sl and crossDn ? crossDn : na, title="Sell Signal Strict Criteria", char='S', location=location.abovebar, color=color.red, transp=0, offset=0)
plotchar(sacl and crossUpAll ? crossUpAll : na, title="Buy Signal Any Cross Up", char='B', location=location.belowbar, color=color.lime, transp=0, offset=0)
plotchar(sacl and crossDownAll ? crossDownAll : na, title="Sell Signal Any Cross Down", char='S', location=location.abovebar, color=color.red, transp=0, offset=0)
alertcondition (crossUpAlert, "higher high")
alertcondition (crossDnAlert, "lower low")
// Debugging.
plotchar(crossUpAll, "crossUpAll", "•", location.bottom, transp = 0)
plotchar(crossDownAll, "crossDownAll", "•", location.top, transp = 0)
plotchar(crossUpAlert, "crossUpAlert", "▲", location.bottom, transp = 60)
plotchar(crossDnAlert, "crossDnAlert", "▼", location.top, transp = 60)
在C#上,我将此偏移量映射到具有相同偏移量的第一个时区:
var dateString = new Date();
var offset = dateString.getTimezoneOffset();
这为我们提供了第一个时区,它具有从客户端接收到的相同偏移量。由于存在多个具有相同偏移量的时区,因此它并不总是与用户的确切时区匹配,但是从UTC转换为本地时间表示是完全可靠的。
我希望它能对某人有所帮助:)