It's about displaying a user's local date and times no matter where the user is.
Just like when I login into aspforums.net, I get to see my local time and date but I know that it is not the same date and time of aspforums server.
I have been shown how to convert into the user's local time.
But to be honest, it did not work. Then I tried another means.
Please I need your help, I beg anyone to help on this with clear details of how I can learn how to this. Thank you
I have tried and tested all the Id of the TimeZone From this Microsoft’s TimeZone Index Values here and I did not get the date and time in local
https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms912391(v=winembedded.11)
When user login the previous date and time is save in Session, and fetched from Session on next page.
The Date and Time is saved as DateTime.UtcNow
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
string check = "SELECT LastLogin from Users WHERE Id = @Id";
using (SqlCommand cmd = new SqlCommand(check, con))
{
cmd.Parameters.AddWithValue("@Id", Session["user"]);
Session["LastLogin"] = Convert.ToDateTime(cmd.ExecuteScalar());
}
string UpdateLog = @"UPDATE Users SET LastLogin=@dateandtime WHERE Id = @Id";
using (SqlCommand cmd4 = new SqlCommand(UpdateLog, con))
{
cmd1.Parameters.AddWithValue("@dateandtime", DateTime.UtcNow);
cmd1.Parameters.AddWithValue("@Id", Session["user"]);
cmd1.ExecuteNonQuery();
con.Close();
}
}
C# Displaying in local time
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"]); // Retrieve server time (stored as UTC)
TimeZoneInfo serverTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); // Server time zone (you can adjust this to match your server's time zone)
TimeZoneInfo userTimeZone = TimeZoneInfo.Local; // User's local time zone
DateTime userLocalTime = TimeZoneInfo.ConvertTime(time1, serverTimeZone, userTimeZone); // Convert to user's local time zone
Timelbl.Text = userLocalTime.ToString("dddd, MMMM d, yyyy h:mm tt");
}
}
}