Hi,
There is no way to do it in C#, you have to get the time offset in javascript:
<script type="text/javascript">
function GetLocalTimeOffset(id)
{
var now = new Date();
var offset = now.getTimezoneOffset();
var hidTimeZone = document.getElementById(id);
if(hidTimeZone != null)
{
hidTimeZone.value = offset;
}
}
</script>
Store it in session by using the hidden field, and after you can get the date with some methods like this (look my helper class):
public static class DateTimeHelper
{
private const string dateTimeStringFormat = "dateTimeStringFormat";
private const string timeStringFromat = "timeStringFormat";
private const string dateStringFormat = "dateStringFormat";
public static string GetDate(DateTime date)
{
string format = ConfigurationManager.AppSettings[dateStringFormat];
return date.ToString(format, new CultureInfo("en-US"));
}
public static string GetDate(DateTime date, double timeZoneOffset)
{
string format = ConfigurationManager.AppSettings[dateStringFormat];
return ConvertUtcToLocal(date, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
}
public static string GetTime(DateTime time, double timeZoneOffset)
{
string format = ConfigurationManager.AppSettings[timeStringFromat];
return ConvertUtcToLocal(time, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
}
public static string GetDateTime(DateTime dateTime, double timeZoneOffset)
{
string format = ConfigurationManager.AppSettings[dateTimeStringFormat];
return ConvertUtcToLocal(dateTime, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
}
private static DateTime ConvertUtcToLocal(DateTime dateTime, double timeZoneOffset)
{
DateTime utcDateTime = dateTime.ToUniversalTime();
return utcDateTime.AddMinutes((-1) * timeZoneOffset);
}
}
Regards
sham