我需要在javascript中实现这个C#方法。需要帮助

时间:2011-09-14 04:44:04

标签: c# javascript jquery asp.net

我在c#.net后面的代码中使用了这个方法。我会将从数据库中获取的日期传递给此方法的参数,然后以非正式格式写入日期。现在发生的事情是我必须在我的jquery代码和

中实现相同的东西

我需要在那里使用这个功能。在我的JavaScript文件中,我将日期作为字符串。现在问题是我不知道如何将theat字符串转换为Date!然后我应该使用该日期并将其传递给此函数?然后做转换,例如这.....(int)s.TotalSeconds;

告诉我如何实现我想要的东西。我是JQuery的新手。

 static string GetInformalDate(DateTime d)
        {
            // 1.
            // Get time span elapsed since the date.
            TimeSpan s = DateTime.Now.Subtract(d);

            // 2.
            // Get total number of days elapsed.
            int dayDiff = (int)s.TotalDays;

            // 3.
            // Get total number of seconds elapsed.
            int secDiff = (int)s.TotalSeconds;

            // 4.
            // Don't allow out of range values.
            if (dayDiff < 0)
            {
                return null;
            }

            // 5.
            // Handle same-day times.
            if (dayDiff == 0)
            {
                // A.
                // Less than one minute ago.
                if (secDiff < 60)
                {
                    return "Just Now";
                }
                // B.
                // Less than 2 minutes ago.
                if (secDiff < 120)
                {
                    return "1 minute ago";
                }
                // C.
                // Less than one hour ago.
                if (secDiff < 3600)
                {
                    return string.Format("{0} minutes ago",
                        Math.Floor((double)secDiff / 60));
                }
                // D.
                // Less than 2 hours ago.
                if (secDiff < 7200)
                {
                    return "1 hour ago";
                }
                // E.
                // Less than one day ago.
                if (secDiff < 86400)
                {
                    return string.Format("{0} hours ago",
                        Math.Floor((double)secDiff / 3600));
                }
            }
            // 6.
            // Handle previous days, months and years.
            if (dayDiff == 1)
            {
                return "Yesterday";
            }
            if (dayDiff < 7)
            {
                return string.Format("{0} days ago",
                dayDiff);
            }
            if (dayDiff < 31)
            {
                return string.Format("{0} week(s) ago",
                Math.Ceiling((double)dayDiff / 7));
            }
            if (dayDiff < 365)
            {
                return string.Format("{0} month(s) ago", Math.Ceiling((double)dayDiff / 31));
            }
            else
            {
                return string.Format("{0} year(s) ago", Math.Ceiling((double)dayDiff / 365));
            }

            return null;
        }

@ IAbstractDownvoteFactory ::我在那个聊天室。如果你还在这里,可以请你来......等待帮助不好:(

有没有其他人可以帮助我?

1 个答案:

答案 0 :(得分:1)

Example

function GetUnformalDate(d) {
    // 1.
    // Get time span elapsed since the date.
    var s = new Date().getTime() - d.getTime();

    // 2.
    // Get total number of days elapsed.
    var dayDiff = (s / 86400000) | 0;

    // 3.
    // Get total number of seconds elapsed.
    var secDiff = s / (1000);

    // 4.
    // Don't allow out of range values.
    if (dayDiff < 0) {
        return null;
    }

    // 5.
    // Handle same-day times.
    if (dayDiff == 0) {
        // A.
        // Less than one minute ago.
        if (secDiff < 60) {
            return "Just Now";
        }
        // B.
        // Less than 2 minutes ago.
        if (secDiff < 120) {
            return "1 minute ago";
        }
        // C.
        // Less than one hour ago.
        if (secDiff < 3600) {
            return Math.floor(secDiff / 60) + " minutes ago";
        }
        // D.
        // Less than 2 hours ago.
        if (secDiff < 7200) {
            return "1 hour ago";
        }
        // E.
        // Less than one day ago.
        if (secDiff < 86400) {
            return Math.floor(secDiff / 3600) + " hours ago";
        }
    }
    // 6.
    // Handle previous days, months and years.
    if (dayDiff == 1) {
        return "Yesterday";
    }
    if (dayDiff < 7) {
        return dayDiff + " days ago";
    }
    if (dayDiff < 31) {
        return Math.ceil(dayDiff / 7) + " week(s) ago";
    }
    if (dayDiff < 365) {
        return Math.ceil(dayDiff / 31) + " month(s) ago";
    }
    else {
        return Math.ceil(dayDiff / 365) + " year(s) ago";
    }

    return null;
}

对于你的日期字符串,我们必须解析:

var digits = "29-07-2011 12:51:33".match(/\d+/g);
var date = new Date(digits[2], digits[1], digits[0], digits[3], digits[4], digits[5])