How do I convert server time to user's time in C#?
I am trying to find a way to display Date and Time based on the user's timezone, but it seems that efforts to make this work is not working.
I want to achieve a situation where Date and Time that is displayed in a user's account in all the pages where the Date and Time is showed; should be displayed in the user's time and not the server time.
I have the Date nd Time saed in UTC in the database.
This is what I tried, but it is showing my country's time which is okay by me. But what if I travel to another country? I want it to display the Date and Time of the country where ever I am.
I used ipstack.com to get the user's TimeZone and using the server time zone I converted to local Date and Time. Then I Installed the TimeZoneConverter package from Nuget.
Please anybody with a better help? I need help in resolving this. Thank you
I am trying to find a way to display Date and Time based on the user's timezone, but it seems that efforts to make this work is not working.
I want to achieve a situation where Date and Time that is displayed in a user's account in all the pages where the Date and Time is showed; should be displayed in the user's time and not the server time.
I have the Date nd Time saed in UTC in the database.
This is what I tried, but it is showing my country's time which is okay by me. But what if I travel to another country? I want it to display the Date and Time of the country where ever I am.
I used ipstack.com to get the user's TimeZone and using the server time zone I converted to local Date and Time. Then I Installed the TimeZoneConverter package from Nuget.
Please anybody with a better help? I need help in resolving this. Thank you
C#:
using System.Net;
using System.Web.Script.Serialization;
using TimeZoneConverter;
public class Location
{
public string IP { get; set; }
public string Country_Code { get; set; }
public string Country_Name { get; set; }
public string Region_Code { get; set; }
public string Region_Name { get; set; }
public string City { get; set; }
public string Zip_Code { get; set; }
public string Time_Zone { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string Metro_Code { get; set; }
}
private Location GetLocation()
{
try
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string url = string.Format("http://api.ipstack.com/{0}?access_key=MY_ACCESS_KEY", ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new Location();
return new JavaScriptSerializer().Deserialize<Location>(json);
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
private void LastLogin()
{
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT LastLogin FROM Users WHERE Id = @Id", con))
{
cmd.Parameters.AddWithValue("@Id", Session["user"]);
con.Open();
DateTime time1 = Convert.ToDateTime(Session["LastLogin"]);
TimeZoneInfo serverTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time");
TimeZoneInfo userTimeZone = TZConvert.GetTimeZoneInfo(this.GetLocation().Time_Zone);
DateTime userLocalTime = TimeZoneInfo.ConvertTime(time1, serverTimeZone, userTimeZone);
Timelbl.Text = userLocalTime.ToString("dddd, MMMM d, yyyy h:mm tt");
}
}
}