Search code examples
sql-serverdatetimegetdate

What will be timezone in central time zone machine, when a asp.net (aspx) page is called from eastern timezone server


I have a SQL Server 2016 in Eastern timezone and there is a stored procedure and it has a call to GETDATE(). ASP.NET page calls this stored procedure; the ASP.NET app is deployed on a server also in Eastern timezone.

If the user accesses the page from Central time zone and will GETDATE() retrieve and show Central timezone date&time, or Eastern timezone date&time?


Solution

  • GETDATE() is executed on your database server, which, if deployed in the Eastern timezone always returns the Eastern time to any client (web) application anywhere in the world.

    So IMHO when dealing with multiple timezones it is better to choose to only store UTC/GMT date/time values in the database and have the clients convert that and display their local date/time. Example:

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("SELECT GETUTCDATE() AS UTC", connection);
            using (SqlDataReader sqlDataReader = cmd.ExecuteReader())
            {
                while (sqlDataReader.Read())
                {
                    DateTime utcFromDatabase = (DateTime)sqlDataReader["UTC"];
                    DateTime mylocalTime = utcFromDatabase.ToLocalTime();
                    myLabel.Text = string.Format("UTC:{0}, Local:{1}", utcFromDatabase, mylocalTime);
                }
            }
        }
    

    Obviously you should not forget to have the clients submit their datetime values as UTC (not local) as well. But using this approach enables you to move your database server to an entire different continent without client datetime formatting issues.