将时区小时差添加到时间中
我知道有更好、更正确的方法来做到这一点,但我需要一个临时的解决方案。我需要在时间上添加额外的时间,日期也会自动更改。我应该如何更改下面的代码?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能澄清一下代码当前正在做什么以及它应该以不同的方式做什么吗?
我能想到的最简单的方法是使用
Date
对象来:Date
实例。dateInstance.getTime()
获取以毫秒为单位的时间。Date
。这样 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 :Date
instance with your initial time.dateInstance.getTime()
.Date
from the updated time in milliseconds.This way the Date class takes care of working out the new date and time.