将时区小时差添加到时间中

发布于 2024-08-30 15:49:28 字数 1286 浏览 3 评论 0原文

我知道有更好、更正确的方法来做到这一点,但我需要一个临时的解决方案。我需要在时间上添加额外的时间,日期也会自动更改。我应该如何更改下面的代码?

package {
public class getTime {
    private var today:Date=new Date();
    private var hour:uint=today.getHours();
    private var minute:uint=today.getMinutes();
    private var month:uint=today.getMonth();
    private var monthArray:Array=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    private var time:String = getUSClockTime(today.getHours(), today.getMinutes());
    public var dateNtime:String=(time+", " +today.getDate()+" "+monthArray[month]+" "+today.getFullYear());;

    public function getTime() {
    }

    private function getUSClockTime(hrs:uint, mins:uint):String {
        var modifier:String="PM";
        var minLabel:String=doubleDigitFormat(mins);

        if (hrs>12) {
            hrs=hrs-12;
        } else if (hrs == 0) {
            modifier="AM";
            hrs=12;
        } else if (hrs < 12) {
            modifier="AM";
        }

        return (doubleDigitFormat(hrs) + ":" + minLabel + " " + modifier);
    }

    private function doubleDigitFormat(num):String {
        if (num<10) {
            return ("0" + num);
        }
        return num;
    }
}
}

I know there's a much better and correct way to do it, but i need a temporarily solutions. I need to add in extra hours to the time, and the day will change automatically too. How should I change the code below?

package {
public class getTime {
    private var today:Date=new Date();
    private var hour:uint=today.getHours();
    private var minute:uint=today.getMinutes();
    private var month:uint=today.getMonth();
    private var monthArray:Array=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    private var time:String = getUSClockTime(today.getHours(), today.getMinutes());
    public var dateNtime:String=(time+", " +today.getDate()+" "+monthArray[month]+" "+today.getFullYear());;

    public function getTime() {
    }

    private function getUSClockTime(hrs:uint, mins:uint):String {
        var modifier:String="PM";
        var minLabel:String=doubleDigitFormat(mins);

        if (hrs>12) {
            hrs=hrs-12;
        } else if (hrs == 0) {
            modifier="AM";
            hrs=12;
        } else if (hrs < 12) {
            modifier="AM";
        }

        return (doubleDigitFormat(hrs) + ":" + minLabel + " " + modifier);
    }

    private function doubleDigitFormat(num):String {
        if (num<10) {
            return ("0" + num);
        }
        return num;
    }
}
}

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

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

发布评论

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

评论(1

尬尬 2024-09-06 15:49:28

您能澄清一下代码当前正在做什么以及它应该以不同的方式做什么吗?

我能想到的最简单的方法是使用 Date 对象来:

  1. 使用您的初始时间创建一个 Date 实例。
  2. 使用dateInstance.getTime()获取以毫秒为单位的时间。
  3. 添加时区差异(以毫秒为单位)。
  4. 根据更新时间(以毫秒为单位)创建新的Date
  5. 根据需要进行任何字符串操作来格式化日期。

这样 Date 类就可以计算出新的日期和时间。

Can you clarify what the code is currently doing and what it should be doing differently?

The easiest way I can think of to do this would be to use the Date object to :

  1. Create a Date instance with your initial time.
  2. Get the time in milliseconds with dateInstance.getTime().
  3. Add the timezone difference in milliseconds.
  4. Create a new Date from the updated time in milliseconds.
  5. Do whatever string manipulation to format the date as required.

This way the Date class takes care of working out the new date and time.

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