を使用すると、DateTime.Now
サーバーの観点から日付と時刻を取得します。クライアントASP.NET での日付と時刻?
ベストアンサー1
ブラウザ/システムの時間とタイムゾーンを使用するか、ユーザーにタイムゾーンを選択させるかというアイデアが気に入っています。過去のプロジェクトでは、次のようなものを使用しました。
<script language="javascript">
function checkClientTimeZone()
{
// Set the client time zone
var dt = new Date();
SetCookieCrumb("ClientDateTime", dt.toString());
var tz = -dt.getTimezoneOffset();
SetCookieCrumb("ClientTimeZone", tz.toString());
// Expire in one year
dt.setYear(dt.getYear() + 1);
SetCookieCrumb("expires", dt.toUTCString());
}
// Attach to the document onload event
checkClientTimeZone();
</script>
そしてサーバー上で:
/// <summary>
/// Returns the client (if available in cookie) or server timezone.
/// </summary>
public static int GetTimeZoneOffset(HttpRequest Request)
{
// Default to the server time zone
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
int result = (int) ts.TotalMinutes;
// Then check for client time zone (minutes) in a cookie
HttpCookie cookie = Request.Cookies["ClientTimeZone"];
if (cookie != null)
{
int clientTimeZone;
if (Int32.TryParse(cookie.Value, out clientTimeZone))
result = clientTimeZone;
}
return result;
}
または、URL パラメータとして渡して Page_Load で処理することもできます。
http://host/page.aspx?tz=-360
すべてのタイムゾーンが 1 時間単位ではないため、分単位を使用することを忘れないでください。