开发者

I need to implement this C# method in javascript. Need help please

I was using this method in code behind in c#.net. I would pass the date fetched from the database into this method's parameter and then get date written in informal format. Now what is happening is that I have to implement the same thing in my jquery code and

I need to use this function there. In my JavaScript file I am getting the date as a string. Now the problem is that I have no clue how to convert theat string into a Date! Then hpw should I use that date and pass it on to this function? Then do conversions for example this .....(int)s.TotalSeconds;

Give me some ideas how I can implement what I want. I am new to JQuery and stuff.

 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 :: I am in that chat room. can you please come if your still here..wa开发者_开发技巧iting..need help bad :(

Is there anyone else who can help me out please??


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

As fpr your date string, we have to parse that:

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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜