1

I have created a WCF Rest service for checking and validating license by comparing date. This service uses for Microsoft Dynamics CRM. CRM user can be from any country.

I have faced few issue since few days especially in the conversation of Date and comparing of it.

Service provides this date in "MM/dd/yyyy time" format. i.e 11/4/2027 12:00:00 AM. It is in the string. I want to convert it in DateTime format such a way that it should convert according to current DateTime format.

//C# Current code
string strValidUpToDate = "11/4/2027 12:00:00 AM";
Date validUpToDate = Convert.ToDateTime(11/4/2027 12:00:00 AM);

Above doesn't provide appropriate format if country changed.

Can anybody please guide me?

Nanji Mange
  • 1,955
  • 3
  • 26
  • 52

1 Answers1

0

CRM stores date time in UTC in database tables. CRM user maybe in different countries (timezone) but the system will calculate it based on user settings & store in offset. When the same data is retrieved inside platform, CRM will reverse engineer it & show based on current user settings.

If the same data is queried in code it will come as it is. ie UTC. You have to take care of timezone conversion in code to get desired results.

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
DateTime localDatetime = TimeZoneInfo.ConvertTimeFromUtc(yourUTCDateTime, tz);
Arun Vinoth - MVP
  • 21,521
  • 14
  • 57
  • 157
  • `TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");` How can I get the time zone identifier '`W. Europe Standard Time`? – Nanji Mange Nov 04 '17 at 06:39
  • Where you are consuming this WCF? – Arun Vinoth - MVP Nov 04 '17 at 06:46
  • It is on Azure. Let me know if you are asking something else. – Nanji Mange Nov 04 '17 at 07:35
  • “according to current datetime format” - based on this point, is it some asp.net website or portal or CRM form or console app or some Azure service where you want to validate? – Arun Vinoth - MVP Nov 04 '17 at 13:03
  • Here, I am bit confuse. As we know CRM account has it's own datetime format and our computer system has it's own datetime. In most cases, both are same. So, I think I need to convert according to convert in datetime format of current system. Please advise. – Nanji Mange Nov 06 '17 at 04:45
  • Yes, in client side or server side Datetime.ToLocalTime will give you local. for comparison convert it to same whatever it is. https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime(v=vs.110).aspx @NanjiMange – Arun Vinoth - MVP Nov 06 '17 at 13:28
  • Read about format specifiers also https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format @NanjiMange – Arun Vinoth - MVP Nov 06 '17 at 13:31