0

I have a c# desktop application that consumes an API. My Client is from a different time zone. So when we both consume the same API, we get different date time even the date is the same in the database. How can I set the default time zone for my application in c# code so that when I access it from the development environment, I can set my timezone and when I deploy it in production then I can set my client's time zone? I already debugged and found .NET automatically converts the time as soon as it gets the response from the API, so I can not do anything in the response stream.

Sayed Uz Zaman
  • 606
  • 1
  • 9
  • 25
  • 3
    Save UTC format in database and return the same, display the dates in client according to the client's culture format – sujith karivelil Apr 25 '19 at 04:59
  • @sujithkarivelil: That's not always the right thing to do. It's widely stated as if it's a silver bullet, but if the date/time is in the future, you could well be losing information, causing the data to become invalid if the time zone rules change. See https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/ – Jon Skeet Apr 25 '19 at 05:46
  • Fundamentally we don't have enough information to answer this question, because it depends on the API. How do you access this API? Does it have any time zone support? Please provide more information. – Jon Skeet Apr 25 '19 at 05:47
  • No, the API does not have any time zone info. It saves time in the database in '2019-04-25 08:54:11' format. – Sayed Uz Zaman Apr 25 '19 at 06:13
  • 1
    It is possible only we get client's time zone. We can get this information from client's browser. – Thamarai T Apr 25 '19 at 06:45
  • Use this code. I want to help this code for you https://stackoverflow.com/a/246529/9243258 – Pr0mis PAtel Apr 25 '19 at 07:22
  • @user269156, do you get an answer? – Thamarai T Apr 30 '19 at 07:36

3 Answers3

0

if the dateTime is changing for you and your client, it means that there is an offset value attached to the dateTime when it is retrieved from the API.

So, do a check for development environment in your application and fetch the UTC DateTime using "UtcDateTime" property of DateTimeOffset type and display it in the UI so that it is the same for both you and your client. In case of the production environment, just keep the usual flowing going.

if(IsDevEnvironment)
   DateTimeToDisplay = ApiResponse.DateTimeInDB.UtcDateTime;  // assuming DateTimeInDB is of DateTimeOffset type
else
   DateTimeToDisplay = ApiResponse.DateTimeInDB
akg179
  • 1,287
  • 1
  • 6
  • 14
-1

Here is a way to set a timezone, example eastern timezone.

TimeZoneInfo easternZone;
try { easternZone = TimeZoneInfo.FindSystemTimeZoneById("E. Africa Standard Time"); }
catch (TimeZoneNotFoundException) { easternZone = TimeZoneInfo.FindSystemTimeZoneById("Africa/Nairobi"); }
var timestamp = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, easternZone);
Fredrick
  • 513
  • 4
  • 17
  • I have understood your answer but the problem is, as soon as my application gets the response, it converts the date time and the converted date time is less or ahead of one day from its the actual value. So how can I tell the .NET not to convert the date time automatically? – Sayed Uz Zaman Apr 25 '19 at 08:35
  • I was responding to the title of how to set a timezone, maybe the title is misleading. The API requires what timezone? – Fredrick Apr 27 '19 at 10:01
-2

try that ;

HAVING        (Time BETWEEN DATEADD(d, - 1, GETUTCDATE()) AND GETUTCDATE())

it is helpful for me.

example

ELECT        Time, username, COUNT(username) AS CountOfusername
FROM            dbo.tabl
GROUP BY Time, username
HAVING        (Time BETWEEN DATEADD(d, - 1, GETUTCDATE()) AND GETUTCDATE())

edit

 DateTime hwTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Arab Standard Time");
        //DateTime hwTime = new DateTime();
        try
        {
            TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Arab Standard Time");
            Console.WriteLine("{0} {1} is {2} local time.",
                    hwTime,
                    hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName,
                    TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
        }
        catch (TimeZoneNotFoundException)
        {
            Console.WriteLine("The registry does not define the Arab Standard Time zone.");
        }
        catch (InvalidTimeZoneException)
        {
            Console.WriteLine("Registry data on the Arab Standard Time zone has been corrupted.");
        }

us this code in your application

ahmed_eltot93
  • 118
  • 16