How do I convert the UTC date time to local time Jquery

jr041283

New Member
I insert 2012-12-11 1pm (I am in Ontario which is EST) into a web form and post it to my local web server, and convert it to UTC time (Asp.Net start_date = CDate(start_date).ToUniversalTime ) before I save it in the database. The UTC time is 6pm in the database field. I use the following code (see below) to convert the saved UTC date time to the user and it happily displays 1pm (GMT-5:00).When I post the same time of 1pm on my production server which is in california the saved time in the database is 9pm. So GMT-8:00 should give me 1pm on the client's browser. The time displayed via the production server is 7pm? Why and is there a fix?I return the value from the database via an asp.net json and then use moment.js to convert it to a UTC number and then run the utcToLocal function. How do I display the expected 1pm? \[code\] function utcToLocal(utc) { // Create a local date from the UTC string var t = new Date(Number(utc)); // Get the offset in ms var offset = t.getTimezoneOffset() * 60000; // Subtract from the UTC time to get local t.setTime(t.getTime() - offset); // do whatever var d = [t.getFullYear(), t.getMonth(), t.getDate()].join('/'); d += ' ' + t.toLocaleTimeString(); return d; } //.format("YYYY-MM-DD h:mm a") function Get_History(filter_date, msg) { //div_history var jsonText = JSON.stringify({ filter_date: filter_date, UserID: userid }); $.ajax({ type: "POST", url: "cc_m.aspx/getHistory", data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d != "0") { var obj = $.parseJSON(data.d); $('.div_history').html(''); $.each(obj, function (index, value) { $('.div_history').append( "<div class='grid_history' style='border: solid 1px silver;'>" + "Group: " + value.Group_Name + "<br/>" + "Teacher: " + value.Teacher + "<br/>" + "Child: " + value.Child_Name + "<br/>" + "Category: " + value.Category + "<br/>" + "Item: " + value.Item + "<br/>" + "From: " + utcToLocal(moment.utc(value.Start_Date)) + "<br/>" + "To: " + utcToLocal(moment.utc(value.End_Date)) + "<br/>" + "Note: " + value.Other + "<br/>" + "Status: " + value.Status + "<br/>" + "<fieldset data-role='controlgroup' data-type='horizontal' data-mini='true' >" + "<button class='send_item' rel='" + value.id_Group + "' data-icon='envelope' data-theme='b' >Send</button>" + "<button class='edit_item' rel='" + value.id + "' data-icon='edit' data-theme='a' >Edit</button>" + "<button class='reset_item' rel='" + value.id + "' data-icon='repeat' data-theme='a' >Reset</button>" + "<a data-role='button' class='delete_item' rel='" + value.id + "' data-icon='remove' data-theme='a' href='http://stackoverflow.com/questions/13829844/#popup_delete' data-rel='dialog'>Delete</a><br/>" + "</fieldset></div><br/>" ); $(".div_history").trigger("create"); //on add history update msg at top of page to NEW RECORD! or ACTIVITY RECORDS SENT! or ACTIVITY RESET TO PENDING! and scroll to top //if (msg.length > 0) { $(".grid_msg").html(msg); //} }); } } //end success }); }\[/code\]
 
Back
Top