Parsing a string into javascript date object based on locale

Last post 10-15-2009 10:18 AM by tysonkb. 2 replies.

Sort Posts:

  • Parsing a string into javascript date object based on locale

    10-14-2009, 2:59 AM
    • Member
      point Member
    • tysonkb
    • Member since 09-22-2009, 5:54 PM
    • Posts 4

    Hi,

    I have got a text box where the user enters a date, for example for the 20th of Jan - my American users will enter '1/20/2009' and my Australian users will enter '20/1/2009'. How can I parse these different string formats into the same javascript Date object so I can work with it on the client? Can I use the microsoft AJAX library with MVC (asp:ScriptManager EnableScriptGlobalization="true")?

    Thanks!

  • Re: Parsing a string into javascript date object based on locale

    10-15-2009, 6:03 AM
    Answer

    Hi,

    Maybe you can take a look at the following code. But firstly you need to download the jquery-ui-1.7.2.custom.min.js from Jquery.

    Firstly we need to import 2 scripts and a css file.

     
        <link href="../../css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    
        <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    
        <script src="../../Scripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>



    Then

    <script type="text/javascript" language="javascript">
       
        $(document).ready(function() {
       
        $('#DateReleased').datepicker({
            duration: '',
            showTime: true,
            constrainInput: false
        });
    
        }
      )
    </script>
    
    
    <label for="DateReleased">DateReleased:</label>
                    <%= Html.TextBox("DateReleased",null, ViewData["Readonly"] as IDictionary<string, object>)%>


    Dictionary<string, object> ID = new Dictionary<string, object>();
                
                ID.Add("readonly", "readonly");
                ViewData["Readonly"] = ID;


  • Re: Parsing a string into javascript date object based on locale

    10-15-2009, 10:18 AM
    Answer
    • Member
      point Member
    • tysonkb
    • Member since 09-22-2009, 5:54 PM
    • Posts 4

    I worked it out. I can use Microsoft AJAX library with globalisation support, however as it is a MVC application I can't use the asp:ScriptResource tags.


    I found this post:

    http://stackoverflow.com/questions/817046/what-about-script-globalization-of-microsoftajax-js-in-asp-net-mvc


    you can then use the date format functions in the microsoft AJAX library to do string to date conversions and date to string based on the user's locale.

Page 1 of 1 (3 items)