【发布时间】:2010-02-08 03:32:17
【问题描述】:
我需要能够从当前日期中减去 2 小时、8 小时、1 天和 1 周。
然后转换为 yyy-mm-dd hh:mm:ss 格式。
到目前为止,我一直没有成功。
在 actionscript 中执行此操作的正确方法是什么?
【问题讨论】:
标签: actionscript-3 date
我需要能够从当前日期中减去 2 小时、8 小时、1 天和 1 周。
然后转换为 yyy-mm-dd hh:mm:ss 格式。
到目前为止,我一直没有成功。
在 actionscript 中执行此操作的正确方法是什么?
【问题讨论】:
标签: actionscript-3 date
这里有几个选项,但我认为在您的情况下最简单的解决方案是使用毫秒。您可以使用 += 和 -= 修改日期的当前毫秒值。最棘手的事情是将您的值转换为毫秒。以下是几个例子:
var myDate:Date; //assuming this is a actual date value.
//subtract 2 hrs
var twoHoursInMilliseconds:int = 2 * 60 * 60 * 1000; //2 hours * 60 minutes * 60 seconds * 1000 milliseconds (in a second)
myDate.milliseconds -= twoHoursInMilliseconds;
//subtract 1 day
var oneDayInMilliseconds:int = 1 *24 * 60 * 60 * 1000;
myDate.milliseconds -= oneDayInMilliseconds;
对于格式化,您将需要使用以下方法:
trace(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
希望这能为您指明正确的方向,
编码愉快!
编辑:更新代码以修复错误。
【讨论】:
我同意 Tyler 以毫秒的形式处理日期的方法。 对于转换,您可能还喜欢使用 DateFormatter,如下所示。
var dateFormatter:DateFormatter = new DateFormatter(); dateFormatter.formatString = 'yyy-mm-dd hh:mm:ss' ; var formattedDate:String = dateFormatter.format(d); 跟踪(格式化日期);
祝愿, 灰烬。
【讨论】:
只需更改日期/小时的 UTC 值。
var d:Date = new Date();
d.dateUTC -= 1; //subtract one day
d.hoursUTC -= 1; //subtract one hour
然后使用 Tyler 的跟踪语句跟踪输出。
trace(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
有关 Date 类的更多信息,请查看http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Date.html
【讨论】: