格式化 Kendo ASP.NET MVC 网格中的日期列

Formatting date column in Kendo ASP.NET MVC Grid

如何在 Telerik Kendo ASP.NET MVC 网格中将此日期格式化为:TodayYesterday<date>

columns.Bound(c => c.DesktopActivation)
                .ClientTemplate("#= DesktopActivation ? renderActivationInfo(DesktopActivation) : '<span style=\"color:\#999999\">Unused</span>' #");

<script type="text/javascript">
   function renderActivationInfo(activationData) 
   {
      return activationData.MachineName + kendo.toString(new Date(parseInt(activationData.DateActivated.substr(6))), 'MMM d, yyyy');
   }
</script>

似乎无法在 Javascript 部分中使用此 C# 实用程序方法:

    public static string GetSpecialDate(DateTime date)
    { 
        int days = ((TimeSpan)(DateTime.Now - date)).Days;

        switch(days)
        { 
            case 0:
                return "Today";
            case 1:
                return "Yesterday";
            default:
                return date.ToString("MMM dd, yyyy", CultureInfo.InvariantCulture);
        }
    }

您只需调整 javascript 函数即可了解 c# 方法的作用。这可能需要针对您的特定对象进行调整,但这将使您朝着正确的方向前进:

function renderActivationInfo(activationData) 
{
    var date = kendo.parseDate(activationData.DateActivated).setHours(0, 0, 0, 0);
    var today = new Date().setHours(0, 0, 0, 0);
    var yesterday = new Date();
    yesterday = yesterday.setDate(yesterday.getDate() - 1);
    yesterday = new Date(yesterday).setHours(0, 0, 0, 0);

    if (date == today) {
        return activationData.MachineName + "Today";
    }

    if (date == yesterday) {
        return activationData.MachineName + "Yesterday";
    }

    return activationData.MachineName + kendo.toString(kendo.parseDate(activationData.DateActivated), "MMM dd, yyyy");
}