开发者

Adding time zone hours difference to time

开发者 https://www.devze.com 2022-12-28 02:05 出处:网络
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

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();
    开发者_StackOverflow社区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;
    }
}
}


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.

0

精彩评论

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

关注公众号