Search code examples
c#javascriptasp.netdatejs

I'm using datejs in an ascx, but it's causing problems for my ajax calendar extender


I'm using the below code to call the correct culture datejs file:

<script 
type="text/javascript" 
src="<%= ResolveUrl("~/Scripts/Date/date-" + System.Globalization.CultureInfo.CurrentCulture.Name + ".js") %>"></script>

I then use the datejs to do some culture specific formatting in a function:

dateTextbox.value = today.toString(Date.CultureInfo.formatPatterns.shortDate);

The reason for this function is because I have a textbox/calendar-extender for selecting a date, and a textbox for entering a number of days. A change to one causes the other to update client side to keep them in sync.

My problem is that when I am using datejs, it overrides the original Date type which is needed in calendar-extender, so if I now click my textbox to select a date, then it throws because of type discrepancy.

Is there a way to make it only use the datejs in my functions, rather than having it throughout the whole control/page, or can I turn it off (so to speak) after it's been used, or any other solutions?


Solution

  • I gave up...decided not to use datejs, but instead am now using my own function in a new script which I'll build upon as different languages are required.

    Here is the function I'm now using:

    //convert a date into a string in a culture specific format
    function FormatDate(thisDate, culture) {
    
        var date = thisDate.getDate();
        var month = thisDate.getMonth() + 1;
        var year = thisDate.getFullYear();
    
        var strDate = date.toString();
        if (strDate.length == 1)
            strDate = ("0" + strDate);
    
        var strMonth = month.toString();
        if (strMonth.length == 1)
            strMonth = ("0" + strMonth);
    
        culture = culture.toLowerCase();
    
        switch (culture) {
            case "en-gb":
                return (strDate + "/" + strMonth + "/" + year.toString());
                break;
    
            case "en-us":
                return (strMonth + "/" + strDate + "/" + year.toString());
                break;
    
            default:
                return (strDate + "/" + strMonth + "/" + year.toString());
                break;
        }
    }
    

    and here is how I call it:

    var today = new Date();
    today = today.getDateOnly();
    today.setDate(today.getDate() + commentPeriod);
    
    dateTextbox.value = 
        FormatDate(today, "<%= System.Globalization.CultureInfo.CurrentCulture.Name %>");