1

I have date time stored in document db database as

"myDate": "2017-06-27T15:44:35.6752016+09:30",

and my property in C# to store that datetime is below

public DateTime MYDate{ get; set; }

On my screen user can click a button which will save the information in a text file. Now information that I want to store in that text file is MyDate value as a string and another version of MyDate as a string also. This another version of MyDate can be of a different timezone. For example, I create MyDate in Australia, Melbourne now lets say I moved to America and when I click that button on client I am passing DateTimeOffset information and want to see that original date and another version in AmericanTime equivalent time. How would I solve this issue?

In .net I know we can use TimeZoneInfo.FindSystemTimeZoneById("id") but how would I pass that id information from javascript client side?

Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
  • Possible duplicate of [How to convert DateTime in Specific timezone?](https://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone) – NineBerry Jul 13 '17 at 10:20
  • In that solution they are calculation it on the basis of config, GetFromConfig.ManilaTimeZoneKey(), which is stored and can be managed by them in my case it can be anything so I have to pass it from client side which is supported in .net – Atul Chaudhary Jul 13 '17 at 10:23
  • The answer is still to use TimeZoneInfo for conversion of datetimes between different time zones. – NineBerry Jul 13 '17 at 10:28
  • I can see that but where I am confused is how do I pass SystemTimeZoneId from javascript client side? – Atul Chaudhary Jul 13 '17 at 10:32

1 Answers1

1

I think I have found the best possible solution which will work for any client using my app from any time zone. To achieve this I am using two library

1) moment timezone - to get the client side timezone(iana format) 2) TimeZoneConverter - c# nuget package written by Matt(datetime expert). This is required to convert iana time zone to window supported timezone

Usage

from client side we can pass iana base timezone using moment timezone like below

const keyName = moment.tz.guess();

then on c# side all we have to do

var clientTimeZone = TZConvert.IanaToWindows(keyName);

var windowsStandardTime = TimeZoneInfo.GetSystemTimeZones().First(x => x.Id == clientTimeZone);

var clientTime = DateTime.SpecifyKind(
                TimeZoneInfo.ConvertTime(dateTimeValue, windowsStandardTime),
                DateTimeKind.Utc);

Hopefully it will help others

Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51