0

我正在使用下面的代码来调用正确的文化 datejs 文件:

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

然后我使用 datejs 在函数中进行一些特定于文化的格式化:

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

这个功能的原因是因为我有一个用于选择日期的文本框/日历扩展器,以及一个用于输入天数的文本框。对其中一个的更改会导致另一个更新客户端以使它们保持同步。

我的问题是,当我使用 datejs 时,它会覆盖日历扩展器中所需的原始 Date 类型,所以如果我现在单击我的文本框来选择一个日期,那么它会因为类型差异而抛出。

有没有办法让它只在我的函数中使用 datejs,而不是在整个控件/页面中使用它,或者我可以在使用它之后关闭它(可以这么说),或者任何其他解决方案?

4

1 回答 1

0

我放弃了...决定不使用 datejs,而是现在在一个新脚本中使用我自己的函数,因为需要不同的语言,我将在此基础上进行构建。

这是我现在使用的功能:

//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;
    }
}

这就是我所说的:

var today = new Date();
today = today.getDateOnly();
today.setDate(today.getDate() + commentPeriod);

dateTextbox.value = 
    FormatDate(today, "<%= System.Globalization.CultureInfo.CurrentCulture.Name %>");
于 2013-10-01T10:38:23.800 回答