locked
How to pass C# variable to javascript variable? RRS feed

  • Question

  • Hi

    In VC# code, it gets the current date time from system

    protected System.DateTime CurDateTime;

    protected string SysDateTime;

    this.CurDateTime = System.DateTime.Now;

    this.SysDateTime = this.CurDateTime.ToString("month dd, yyyy HH:mm:ss");

    I have a javascript variable needs to get the SysDateTime value in aspx page. 

    var currenttime = <%= SysDateTime %>;

    But, it cannot get it, how should I write it?


    • Moved by Peter Ritchie Wednesday, November 5, 2008 3:08 PM ASP.NET, HTML question (Moved from Visual C# General to Off-Topic Posts (Do Not Post Here))
    Wednesday, November 5, 2008 9:19 AM

Answers

  • Make sure that you quote your javascript variable currentTime.


    var currentTime = '<%= SysDateTime %>';

    John Hidey - Dont forget to mark your threads when they are answered or helpful
    • Marked as answer by Tommy2008 Wednesday, November 5, 2008 12:33 PM
    Wednesday, November 5, 2008 11:39 AM

All replies

  • Make sure that you quote your javascript variable currentTime.


    var currentTime = '<%= SysDateTime %>';

    John Hidey - Dont forget to mark your threads when they are answered or helpful
    • Marked as answer by Tommy2008 Wednesday, November 5, 2008 12:33 PM
    Wednesday, November 5, 2008 11:39 AM
  • It shows compilation error. The name 'SystemDateTime' does not exist in the current context
    Wednesday, November 5, 2008 12:30 PM
  • oh, sorry, wrong spelling of systemdatetime. It works now. Thanks
    Wednesday, November 5, 2008 12:33 PM