0

I have a variable in razor which has time value. I want to change it to the correct client's local time. I cannot use the variable in javascript code...

<script type="text/javascript">                             
    var tm =  @arraystr[9] ;
    var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
    var offset = date.getTimezoneOffset() / 60;
    var hours = date.getHours();
    newDate.setHours(hours - offset);
    @arraystr[9] = newDate;
</script>

<div class="mtime">@arraystr[9]</div>

THis is code in the partial View called by Index View... An alternative way, is to use javascript code in Index View to change the time value in all div tags where class="mtime"... the javascript code is called before body tag ends:

<script type="text/javascript">
            var divs = document.getElementsByClassName('mtime');
            [].slice.call(divs).forEach(function (div) {

                var date = div.InnerHTML;
                var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);

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

                newDate.setHours(hours - offset);
                div.InnerHTML = newDate;

            });


        </script>

    </body>

nothing of these 2 works...

MVCnewbie
  • 15
  • 1
  • 8
  • Please ask google first.. – greenhoorn May 11 '15 at 09:40
  • 1
    possible duplicate of [Mix Razor and Javascript code](http://stackoverflow.com/questions/5614941/mix-razor-and-javascript-code) – greenhoorn May 11 '15 at 09:40
  • I have viewed many similar questions and solutions, nothing worked... – MVCnewbie May 11 '15 at 09:47
  • I'm not sure if it is possible to achieve this if the javascript is located within external file. At least not without using a call to controller which would then return time. – wegelagerer May 11 '15 at 09:51
  • 2
    Razor code is parsed on the server **before** its sent to the view. Javascript is client side code. Razor code cannot access a javascript variable that does not even exist yet! –  May 11 '15 at 09:51
  • the code above is in partial view called by index view. I could call the function for all the mtime class divs in index... – MVCnewbie May 11 '15 at 10:01
  • I have to assume you've got a `DateTime` in .net. In this case you have to wrap it in quotes, i.e. `var myDate = '@myVar';` and then parse it, i.e. with moment (if necessary) and eventually replace the content of the elements with `mtime` as class. – Jack May 11 '15 at 10:19
  • yess, I ll better ask a new question and explain things more clear.. – MVCnewbie May 11 '15 at 10:26

1 Answers1

0

To set c# variable into javascript try syntax as below:

var jsVar = "@cSharpVar";
zooblin
  • 2,172
  • 2
  • 27
  • 33
  • thanks, but I ll better ask a new question and give details in a more appropriate way – MVCnewbie May 11 '15 at 10:40
  • as Stephen said it will not work because i expect javascript code to be executed with razor at the same time – MVCnewbie May 11 '15 at 10:41
  • you are right, It's impossible, because javascript code run after page rendered on server, but in this way you can initialize your javascript variables – zooblin May 11 '15 at 10:44