DateTime issues with Azure c#, javascript, sql....so confused.http://forums.asp.net/t/1808269.aspx/1?DateTime+issues+with+Azure+c+javascript+sql+so+confused+Thu, 23 Aug 2012 06:38:55 -040018082695000941http://forums.asp.net/p/1808269/5000941.aspx/1?DateTime+issues+with+Azure+c+javascript+sql+so+confused+DateTime issues with Azure c#, javascript, sql....so confused. <p>Alright, so my website allows people to store their work schedules. &nbsp;They enter their shift starting datetime and ending datetime. &nbsp;Simple enough, right?</p> <p>The idea is I can look at the site and see Bob's working in Chicago tonight from 4-6pm, and Joe's working in New York from 8-10pm. &nbsp;And that's how I want the dates displayed in the app. &nbsp;If I'm viewing the site from California, I still want to display Chicago times for Bob, and New York times for Joe. &nbsp;<strong>I do NOT want to convert those times to California times where it'd show Bob working from 2-4pm, and Joe working from 5-7pm.</strong></p> <p>My site has sample data and when everything's located in the same timezone it's fine, but I've now deployed it to Azure servers in North Central US (It uses SQL Azure located there as well, although I'm unclear on where there is), and now the date and times are all off (jumped forward by 7 hours).</p> <p>I believe the reason for this is that I use a lot of Ajax and JSON, and JSON requires datetimes to be formatted like this <span>/Date(1240718400000)/, where the number represents the milliseconds since epoch/</span>Unix time. &nbsp;I display the schedules in a Calendar/Week/or Day view, which requires a lot of javascript datetime comparisons.</p> <p>Anyway, anyone know the correct way to do what I need to do? &nbsp;I'm thinking I need to store the datetimes as UTC, but I also need to know the timezone where the user's work shift will take place, and then I need to adjust it on the C# side, but then do I need to do anything on the javascript side?</p> <p>I'm just confused, seems like too many variables to wrap my head around at the moment.</p> 2012-05-28T18:43:45-04:005001044http://forums.asp.net/p/1808269/5001044.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>I would transfer as strings and handle transformation from string to date on javascript browser.</p> 2012-05-28T22:43:36-04:005002897http://forums.asp.net/p/1808269/5002897.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>Thanks for the response. &nbsp;I think that may work. &nbsp;However, since I also have logic in stored procedures dealing with the shift date times, it would be nice to store it as a date still.</p> <p>I think I came up with a solution, but don't know how to implement it yet. &nbsp;What if I just treated everything as a UTC date. &nbsp;By that I mean, if a user in New York says they work at 2pm on June 1st, I treat it as if the user, the webserver, the database and everything actually exists in the UTC timezone. &nbsp;So the datetime that's stored in the database is &nbsp;2012/06/01 14:00 UTC and no conversion or timezone adjustment is necessary anywhere, and all my web and database code would always be dealing with UTC dates, so a user in California would see that same datetime as 2012/06/01 14:00 as well, which is what I want.</p> <p>I think that would work, but then the question becomes, how do I force the datetime they enter to be treated as if it was entered by a user/system in the utc timezone?</p> <p>I thought I had it, with the SpecifyKind method:</p> <p>DateTime dt = DateTime.SpecifyKind(DateTime.Parse(dateEnteredStr), DateTimeKind.Utc);</p> <p>But this doesn't seem to work. &nbsp;The underlying value of the DateTime object doesn't change. &nbsp;I was hoping this would take a date like &quot;2012/06/01 14:00 <strong>EST</strong>&quot; and make it &quot;2012/06/01 14:00 <strong>UTC</strong>&quot;, but nope. &nbsp;There has to be a way to do this I think.</p> <p></p> 2012-05-29T22:45:15-04:005002908http://forums.asp.net/p/1808269/5002908.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>Oops, my mistake, the problem I have isn't on the C# side (the SpecifyKind function does what I want it to do), it's on the javascript side. &nbsp;I need to somehow create a javascript Date object similar to how I described C#'s SpecifyKind function works.</p> <p>Since I'm dealing with JSON, I need javascript that will create a Date object in the UTC timezone from a given &quot;milliseconds since epoch&quot; value. &nbsp;The problem is that javascript dates automatically convert a UTC datetime value to a local timezone value.</p> <p>Hopefully this code and comments will explain the problem:</p> <p>//&nbsp;1335834000000 =&nbsp;<span>Tue, 01 May 2012 01:00:00 GMT</span></p> <p><span>// The following javascript running on my laptop in California will produce a Date object with a value of<br> //&nbsp;<span>Mon Apr 30 2012 18:00:00 GMT-0700 (Pacific Daylight Time)</span></span></p> <p>var dt = new Date(1335834000000);</p> <p>// I need something like this that produces the original UTC time of<br> // Tue, 01 May 2012 01:00:00 GMT</p> <p>var dt = new Date(1335834000000, &quot;UTC&quot;)</p> 2012-05-29T23:36:52-04:005004590http://forums.asp.net/p/1808269/5004590.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>Alright, figured it out. &nbsp;Had to change a lot of javascript to make all the Date objects use the UTC timezone, but it works.</p> <p>Thanks.&nbsp;</p> 2012-05-30T17:00:00-04:005101995http://forums.asp.net/p/1808269/5101995.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>Hi,</p> <p>&nbsp;</p> <p>i am facing the same problem , when my website access from diffrent PC with diffret time zone the JS Ajax return date is chage to the local time zone, i want to stop it &amp; want to show the real time which come from db, can any one help me out . PLZ</p> 2012-08-09T17:12:55-04:005117790http://forums.asp.net/p/1808269/5117790.aspx/1?Re+DateTime+issues+with+Azure+c+javascript+sql+so+confused+Re: DateTime issues with Azure c#, javascript, sql....so confused. <p>What I did was treat the date as UTC at every layer (javascript front-end, C# middle-tier, and SQL backend).</p> <p>The javascript code is a little tricky, but I did it with the following function which will convert ASP.Net JSON dates, as well as normal date objects and formatted date strings to UTCDates.&nbsp; It also will return a new UTC date for the current datetime if no param is passed.&nbsp; So I pass all my sending of user-entered form date fields through this function before passing it to the C# middle tier via Ajax WebService call.&nbsp; And also pass all my retrieved dates via Ajax/JSON from the WebService through it before displaying to the user.&nbsp; And finally, I use it to get the current date (treated as a UTC date) when doing any javascript logic needing to compare against the current datetime.</p> <p>function createOrConvertUTCDate(dt) {<br> &nbsp;&nbsp;&nbsp; var d, convertDateToUTC;<br> &nbsp;&nbsp;&nbsp; convertDateToUTC = false;<br> &nbsp;&nbsp;&nbsp; if (dt != null &amp;&amp; dt != undefined) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dt.indexOf(&quot;Date&quot;) &gt; 0) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dt = dt.replace(/\\\\/, &quot;\\&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = new Date(parseInt(dt.substr(6)));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = new Date(dt);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertDateToUTC = true;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; else {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = new Date();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertDateToUTC = true;<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; if (convertDateToUTC) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dUTC = new Date();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCMonth(d.getMonth())<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCDate(d.getDate());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCFullYear(d.getFullYear());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCHours(d.getHours());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCMinutes(d.getMinutes());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCSeconds(d.getSeconds());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dUTC.setUTCMilliseconds(d.getMilliseconds());<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = dUTC;<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());<br> }</p> <p>The C# side is easy.&nbsp; I pass the date object or string through the following 2 functions when sending/receiving dates to the database:</p> <p>public static DateTime ConvertDateTimeToUTC(string dtStr)<br> {<br> &nbsp;&nbsp;&nbsp; return DateTime.SpecifyKind(DateTime.Parse(dtStr), DateTimeKind.Utc);<br> }</p> <p>public static DateTime ConvertDateTimeToUTC(DateTime dt)<br> {<br> &nbsp;&nbsp;&nbsp; return DateTime.SpecifyKind(dt, DateTimeKind.Utc);<br> }</p> <p>As dates are passed to the database, they will already be UTC dates, so I just modified any of my stored procedure logic doing date comparisons against the current date to use getUTCDate() instead of getDate()</p> <p>Hope that helps.</p> <p></p> 2012-08-23T06:38:55-04:00