1

I have WebApp deployed on US server. I have used DateTime.Now in order to capture User Date and Time on certain Action by the User.

Localy it works fine and gets the Date time correct. But post the deployment function is saving the date time for the Server Date time and not the User.

Users are in India hence it should capture IST and not MST as what it is doing now.

Here is my Code I have used so far with no success:

//Getting the current UTC Time
DateTime UTCTime = System.DateTime.UtcNow;
//Adding the time difference 5.5 hours to the utc time
DateTime IndianTime = UTCTime.AddHours(5.5);   

dto.ChatCreateDateTime = IndianTime;

Using TimeZone Class did not work either.

TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime timeUTC = DateTime.UtcNow;
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(timeUTC, timeZone);

dto.ChatCreateDateTime = result;

I did try searching online and tried many fix like TimeZone Class, and other but none worked for me.

Use the below code but no correct datetime. Time shows as behind by 30 mins.

DateTime dateTime = DateTime.Now; 
                DateTime utcTime = dateTime.ToUniversalTime(); 
   TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); 
   DateTime yourISTTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, istZone); 

            model.ChatCreateDateTime = yourISTTime;

                Chat dto = new Chat();
                //dto.CustEmail = model.CustEmail;
                dto.CustName = model.CustName;
                dto.ChatStartDateTime = model.ChatStartDateTime;

                // Gets current local date
                // Returns 04/09/12 11:30 in my case
                
                

                dto.ChatStartDateTime = model.ChatStartDateTime;

Thank you in advance.

Riyaz Shaikh
  • 83
  • 11
  • https://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone – Dheeraj Kumar Feb 25 '22 at 03:47
  • Don't use `DateTime` for starters, so you don't have to convert. Use `DateTimeOffset`. Otherwise you'll have similar problems if the server timezone changes. What happens if you use a cloud VM, which are typically in UTC? – Panagiotis Kanavos Feb 25 '22 at 07:26
  • Hello did you able to made any progress or still stuck in? – Md Farid Uddin Kiron Mar 02 '22 at 06:35
  • Does this answer your question? [How to convert DateTime in Specific timezone?](https://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone) – Druid Apr 05 '23 at 11:23

1 Answers1

0

If you want to convert time from UTC to any specific Time Zone you should try follwing way.

   DateTime dateTime = DateTime.Now; // I am getting date time here
   DateTime utcTime = dateTime.ToUniversalTime(); // From current datetime I am retriving UTC time
   TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); // Now I am Getting `IST` time From `UTC`
   DateTime yourISTTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, istZone); // Finally converting it 
   var hourIST = yourISTTime.Hour; // More granular extraction. 

Output:

enter image description here

Note:

I am trying this way and perfectly working for me. If you require further modification and granularity you could check our official document here.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Appreciate your response. Am I missing some thing very imp here, still now working with following code DateTime dateTime = DateTime.Now; // I am getting date time here DateTime utcTime = dateTime.ToUniversalTime(); // From current datetime I am retriving UTC time TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); // Now I am Getting `IST` time From `UTC` DateTime yourISTTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, istZone); // Finally converting it dto.ChatCreateDateTime = yourISTTime; – Riyaz Shaikh Feb 25 '22 at 09:55
  • Thanks for your feedback, I am not clear what you are missing even confused a bit whether you mean its working now or not working. Please share your further concern – Md Farid Uddin Kiron Feb 25 '22 at 10:00
  • No, it is not working, It works fine locally, but does not work on Prod Environment – Riyaz Shaikh Feb 25 '22 at 10:14
  • Here is what I am doing. the code above I use straight in the Controller. Am I suppose to create a seperate model Class for GetDtTime and return it in Controller ? – Riyaz Shaikh Feb 25 '22 at 10:15
  • Yes you can do that as well, But I am curious what happend in `Prod Environment`, do you have the option run this on `prod env` using visual studio? Does your prod mahcine has the date time set up accordingly? – Md Farid Uddin Kiron Feb 25 '22 at 11:16
  • Came close but not updating the correct time. It updates time behind by 30 minutes. Example if now the time is 16:18, with the above updated code it updates as 15:49. Kindly suggest a solution on this. You can refer to the updated comment in my Question. Thank you – Riyaz Shaikh Feb 26 '22 at 10:54
  • Would you kindly share your `dto.ChatStartDateTime` class details. If possible share few demo data as well. Need to have a look how you are collecting/storing the data. – Md Farid Uddin Kiron Feb 28 '22 at 05:45