as3: date.getMinutes >如何添加零? (05:09 上午 = 5:9 上午)

发布于 2024-10-17 11:20:10 字数 480 浏览 6 评论 0原文

如何修复日期以检索带零的分钟/小时?

例如,现在是 05:09 AM >

trace(_date.getHours()+":"+_date.getMinutes());  //5:9

但我想要 05:09 而不是 5:9 - 那么如何添加零?

    var _date = new Date();

    ...

    _min = _date.getMinutes();

    //fix date:
                var _str:String = _min.toFixed(1);
                _min = Number(_str);

    trace(_date.getHours()+":"+_date.getMinutes());

= 5:9 .... -_-

怎么了?

How to fix date to retrieve minutes/hours with zeros ?

e.g. it's 05:09 AM >

trace(_date.getHours()+":"+_date.getMinutes());  //5:9

but i want 05:09 instead of 5:9 - so how to add zeros ??

    var _date = new Date();

    ...

    _min = _date.getMinutes();

    //fix date:
                var _str:String = _min.toFixed(1);
                _min = Number(_str);

    trace(_date.getHours()+":"+_date.getMinutes());

= 5:9 .... -_-

what's wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-10-24 11:20:10

您可以使用以下格式对其进行格式化:

分钟_txt:String = _date.getMinutes() < 10? "0" + _date.getMinutes() : _date.getMinutes();

如果分钟数小于 10,则会填充零,然后只跟踪它而不是仅跟踪 _date.getMinutes

You can format it with something like:

minutes_txt:String = _date.getMinutes() < 10 ? "0" + _date.getMinutes() : _date.getMinutes();

which will pad a zero if the minutes is less than 10, and then just trace that instead of only _date.getMinutes

爱,才寂寞 2024-10-24 11:20:10

就我个人而言,我会编写一个更通用的例程。我用两种方法完成了它:

更传统的方法:

    // Pass in 'value' you want to pad, and 'len' as total length of string 
    // to be returned to you. For example, value=24, len=6 would return 000024.
    public static function padIntWithLeadingZeros2(value:int, len:uint):String
    {
        var paddedValue:String  = value.toString();

        if (paddedValue.length < len)
        {
            for (var i:int = 0, numOfZeros:int = (len - paddedValue.length); i < numOfZeros; i++)
            {
                paddedValue = "0" + paddedValue;
            }
        }

        return paddedValue;
    }   

我自己的做法:

    // Pass in 'value' you want to pad, and 'len' as total length of string 
    // to be returned to you. For example, value=24, len=6 would return 000024.
    public static function padIntWithLeadingZeros(value:int, len:uint):String
    {
        var paddedValue:String  = value.toString();

        if (paddedValue.length < len)
        {
            var leadingZeros:String = "0000000000";
            paddedValue = leadingZeros.substring(0, (len - paddedValue.length)) + paddedValue;
        }

        return paddedValue;
    }

事实证明,就执行函数的时间而言,一种方法与另一种方法一样有效。所以,这只是一个偏好问题。

R·格莱姆斯

Personally, I would write a more generic routine. I've done it two ways:

More traditional approach:

    // Pass in 'value' you want to pad, and 'len' as total length of string 
    // to be returned to you. For example, value=24, len=6 would return 000024.
    public static function padIntWithLeadingZeros2(value:int, len:uint):String
    {
        var paddedValue:String  = value.toString();

        if (paddedValue.length < len)
        {
            for (var i:int = 0, numOfZeros:int = (len - paddedValue.length); i < numOfZeros; i++)
            {
                paddedValue = "0" + paddedValue;
            }
        }

        return paddedValue;
    }   

My own style of doing it:

    // Pass in 'value' you want to pad, and 'len' as total length of string 
    // to be returned to you. For example, value=24, len=6 would return 000024.
    public static function padIntWithLeadingZeros(value:int, len:uint):String
    {
        var paddedValue:String  = value.toString();

        if (paddedValue.length < len)
        {
            var leadingZeros:String = "0000000000";
            paddedValue = leadingZeros.substring(0, (len - paddedValue.length)) + paddedValue;
        }

        return paddedValue;
    }

Turns out that one is as efficient as the other in terms of lapsed time to execute the function. So, it's just a matter of preference.

R. Grimes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文