Flash Date.getTime 错误?
我在这件事上碰壁了。然而,我的脚本找到了两个日期之间的小时数差异。
var data:Array = ["2011-08-30 11:19:19", "01-09-2011 02"];
var aDate:String = data[0].split(" ")[0];
var dateElements:Array = aDate.split("-");
var date1:Date = new Date();
date1.setDate(int(dateElements[2]));
date1.setMonth(int(dateElements[1])-1);
date1.setFullYear(int(dateElements[0]));
date1.setHours(int(data[0].split(" ")[1].split(":")[0]));
trace("date: " + date1.getDate());
trace("month: " + date1.getMonth());
trace("year: " + date1.getFullYear());
trace("hours: " + date1.getHours());
dateElements = data[1].split(" ")[0].split("-");
var date2:Date = new Date();
date2.setDate(int(dateElements[0]));
date2.setMonth(int(dateElements[1])-1);
date2.setFullYear(int(dateElements[2]));
date2.setHours(int(data[1].split(" ")[1]));
trace("__");
trace("date: " + date2.getDate());
trace("month: " + date2.getMonth());
trace("year: " + date2.getFullYear());
trace("hours: " + date2.getHours());
trace("__");
var elapse:Number = date2.getTime() - date1.getTime();
trace(Math.floor(elapse / 3600000));
正如您所看到的,日期元素按其应有的方式绘制。现在尝试将数据数组的第一个元素更改为“2011-08-31 11:19:19”。尽管日期元素很好,但最后一条轨迹给出了一个完全奇怪的值。更奇怪的是,当在Flash IDE中第二次编译这个脚本时,编译时间很长,然后根本没有任何痕迹,就像脚本超时一样。
这里发生了什么事?
I'm hitting a wall on this one. My script finds the difference in hours between 2 dates, however..
var data:Array = ["2011-08-30 11:19:19", "01-09-2011 02"];
var aDate:String = data[0].split(" ")[0];
var dateElements:Array = aDate.split("-");
var date1:Date = new Date();
date1.setDate(int(dateElements[2]));
date1.setMonth(int(dateElements[1])-1);
date1.setFullYear(int(dateElements[0]));
date1.setHours(int(data[0].split(" ")[1].split(":")[0]));
trace("date: " + date1.getDate());
trace("month: " + date1.getMonth());
trace("year: " + date1.getFullYear());
trace("hours: " + date1.getHours());
dateElements = data[1].split(" ")[0].split("-");
var date2:Date = new Date();
date2.setDate(int(dateElements[0]));
date2.setMonth(int(dateElements[1])-1);
date2.setFullYear(int(dateElements[2]));
date2.setHours(int(data[1].split(" ")[1]));
trace("__");
trace("date: " + date2.getDate());
trace("month: " + date2.getMonth());
trace("year: " + date2.getFullYear());
trace("hours: " + date2.getHours());
trace("__");
var elapse:Number = date2.getTime() - date1.getTime();
trace(Math.floor(elapse / 3600000));
As you can see, the date elements trace out the way they should. Now try changing the first element of the data array to "2011-08-31 11:19:19". Although the date elements are fine, the last trace gives a totally weird value. What's even weirder is that when compiling this script a second time in Flash IDE, it takes a long time to compile, then traces nothing at all, as if the script times out.
What's happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在设置日期(日)之前设置月份,则应该给出正确的结果。默认情况下,当您创建 Date 对象时,时间设置为本地(操作系统)日期。现在是 9 月,仅包含 30 天,因此当您将日期设置为 31 时,它会自动转换为 1。如果您将月份定义为 8 月,它应该接受 31 值。为了避免二月的问题,把年份放在第一位可能是个好主意。
If you set the month before you set the date (day) it should give the correct result. By default, when you create a Date object the time is set to the local (Operating System) date. Right now, it's September and it only contains 30 days, so when you set the date to 31 it automatically gets converted to 1. If you define the month as August first it should accept the 31 value. And to avoid february problems it might be a good idea to put the year first.