0

I am working in a client-server architecture. Both my client and server reside outside of a Utc Timezone.

I send a Datetime object in Utc from my client to server. As soon as my Datetime object enters into my server it is default to Local time zone. I can convert back my Datetime object to Utc timezone by calling ToUniversalTime() method but is there any way to tell the server that do not default the Datetime object to its Local timezone?

muhammad kashif
  • 2,566
  • 3
  • 26
  • 49
  • 1
    See this [SO thread](http://stackoverflow.com/q/4331189/304683) – EdSF Oct 09 '13 at 05:49
  • Good SO, There is a comment that for the purpose of CreationDate or updationdate use UTC time and then we can convert it to the user's timezone to display. But my problem is My UTC time defaults to local timezone when it enters from client to server or server to client. From the thread I think they are suggesting to use DateTimeOffset. Am I getting it correct? – muhammad kashif Oct 09 '13 at 06:02
  • 1
    Not sure I completely understand - can you post your code and/or provide more detail - as in how exactly is data sent/consumed? Is your goal "timestamping" some "event"? – EdSF Oct 09 '13 at 14:45
  • Basically We have a server (Asp.net website) hosted under IIS. Then we have our client (asp.net website) hosted under IIS on different machine in different time zones. They both communicate with each other via Hessian Logic. If Server sends Date in UTC format it is automatically default to local time. That is DateTime.Kind property is changed to Local. eg: server sends 10/10/2013 4:15:56 PM UTC while client see it as 10/10/2013 4:15:56 PM Local. As this is message passing between client and server its not possible for me to show you the code. but these are simple datetime objects. – muhammad kashif Oct 10 '13 at 05:51

2 Answers2

0

I think you are looking for:

dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);

But it's really hard to say for sure since you did not show any code in your question.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Actually I do not think code will make any difference. Read my comments on the question for more clarity. I have managed to achieved the desire result by changing UTC Datetime to String and then telling my javascript that this string is a UTC time. I will post my complete answer shortly. – muhammad kashif Oct 14 '13 at 09:45
  • Your comments offer no additional clarity. I've never heard of "Hessian Logic", and you say that the other client is a web server, but here you are talking about javascript which would have to run in a web browser. Sorry, but I have no idea what you are talking about at this point. – Matt Johnson-Pint Oct 14 '13 at 15:53
0

Use DateTimeStyles.AdjustToUniversal when you parse the string. Or DateTimeStyles.RoundtripKind.

(Or don't use DateTime at all; switch to DateTimeOffset.)

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • Problem is not at the serverside. It's jsonserializer which is creating the problem. I am always giving Json serializer a UTC date time but it always change the timezone to local by keeping the date and time same as UTC. see my comments on my questions for more clarity. some how I have managed it. – muhammad kashif Oct 14 '13 at 09:44