0

i have date in pst format in database and i am passing it from backend via C# to javascript and then trying to convert it to client local time and here is the code i am using to convert it

  onverttDate: function (element,date)
        {
           var Element = $(element);
           var d1 = new Date(date);

           var newDate = new Date(d1.getTime() + d1.getTimezoneOffset() * 60 * 1000);

           var offset = d1.getTimezoneOffset() / 60;
           var hours = d1.getHours();

                newDate.setHours(hours  offset);



                Element.append('<div>' + newDate + '</div>');

        }

and the date string i am getting from backend to javascript function is May 24,2014 09:59:59 and the converted date should be May 24,2014 22:59:59

i am not able to spot bug,i tried using moment.js also but its also not working. Please give me a solution for this as what i am doing wrong. Any, js fiddle example hardcoding date string as it is will also be appreciated Note:I am getting date string from ajax call from server

user3631413
  • 101
  • 2
  • 10
  • That code will give you the *current* offset, which isn't necessarily the correct offset for the date in question. But you're not really showing enough code to diagnose further. Also, if you search, you'll find there are plenty of questions with good answers in this area already. – Matt Johnson-Pint Jun 11 '14 at 05:15
  • You may be interested in [this answer](http://stackoverflow.com/a/22625076/634824) and [this answer](http://stackoverflow.com/a/16351529/634824), as well as [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info) and [the dst tag wiki](http://stackoverflow.com/tags/dst/info). – Matt Johnson-Pint Jun 11 '14 at 05:20
  • @MattJohnson in actual i am getting date from server and then want to convert it into client time zone i am getting it in format "MMM dd yyyy HH:mm:ss"); Can i convert it into client timezone using moment.js can u provide me a sample code of converting it into client timezone using it or any other method in javascript. – user3631413 Jun 11 '14 at 06:19
  • No, that's not what StackOverflow is for. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic), and other related topics [in the help center](http://stackoverflow.com/help). – Matt Johnson-Pint Jun 11 '14 at 07:03
  • I see you've made an edit, and I can see that this code won't work. But I'm still not sure what exactly you're *trying* to do. Please answer specifically 1) What is the *exact* string you are passing in? Please use quotes around the string, show spacing, punctuation, etc. Preferably, show the C# code that generates the string as well. 2) You say the server stores the value in PST. Do you really mean PST? PST is fixed to UTC-8, but the US Pacific time zone does not always use PST. It alternates between PST and PDT, which is UTC-7. Please confirm, as it will affect the design of the solution. – Matt Johnson-Pint Jun 12 '14 at 05:13
  • 3) Are you wanting the result in some specific time zone? Or just in the local time zone of the user visiting the web site? 4) Should the output string be in any particular format? Or just some human-readable locale-specific format? – Matt Johnson-Pint Jun 12 '14 at 05:14
  • BTW - your new code is missing an operator between the `hours` and `offset` in the `setHours` call, so this will actually just generate a runtime error. But assuming you meant to show a `+`, then this code would just be shifting the point in time by twice the amount of the local UTC offset. It doesn't actually change the *time zone* at all. – Matt Johnson-Pint Jun 12 '14 at 05:18

0 Answers0