c# - Flex/Flash/Java/C# 中的漂亮日期文本

标签 c# java actionscript-3 string date

是否有免费的库或类可以以漂亮的方式格式化日期,例如“5 分钟前”或“昨天”?

我对可以移植到 Actionscript(如 Java 或 C#)的另一种语言的相同代码感到满意

最佳答案

这有帮助吗?应该很容易移植到 AS3。

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
        diff = (((new Date()).getTime() - date.getTime()) / 1000),
        day_diff = Math.floor(diff / 86400);

    if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
        return;

    return day_diff == 0 && (
            diff < 60 && "just now" ||
            diff < 120 && "1 minute ago" ||
            diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
            diff < 7200 && "1 hour ago" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
        day_diff == 1 && "Yesterday" ||
        day_diff < 7 && day_diff + " days ago" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
    jQuery.fn.prettyDate = function(){
        return this.each(function(){
            var date = prettyDate(this.title);
            if ( date )
                jQuery(this).text( date );
        });
    };

关于c# - Flex/Flash/Java/C# 中的漂亮日期文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760927/

相关文章:

c# - 数据绑定(bind)文本框不反射(reflect)源更改

java - 从字符串中获取子字符串 Java(Android)

actionscript-3 - 如何循环播放子项但仅作为 MovieClip?

java - 为什么带有返回类型的 Java 方法引用与 Consumer 接口(interface)匹配?

java - 为什么 executeUpdate() 函数不起作用?给出在普通项目而不是基于 maven 的项目中解决的步骤

actionscript-3 - ActionScript 编码标准

flash - 创建一个大型二维数组并将其填充到 AS3 中

c# - 使用ASP.MVC编辑树状结构

c# - C/C++ 全局变量相当于什么?

C# Web 服务客户端转换错误