How to do Flex date deduction and addition

左心房为你撑大大i 提交于 2019-12-22 04:36:09

问题


In flex, I am trying to do date deduction and addition, but couldn't find a way to do it.

e.g.: public var dateNow:Date=new Date();

How can I get the Date 3 months earlier than dateNow?

Thanks!!!


回答1:


You can use the Date constructor for this. The first argument to Date's constructor takes either a year or a timestamp. You can use the Date.time property to get the timestamp from a date object. Once you have the timestamp you can add/subtract some number of seconds from it, and then pass it to new Date(timestamp) and you get a brand new date which represents the new timestamp.

Edit; As a commenter pointed out, time manipulation may not be the best way to go. But you can still use the Date constructor as follows:

var now:Date = new Date();
var threeMonthsAgo = new Date(now.fullYear, 
                              now.month - 3,  
                              now.date, 
                              now.hour, 
                              now.minute, 
                              now.second, 
                              now.millisecond);

The Date constructor is smart enough to deal with negative values, or values greater than 11.




回答2:


Try the DateUtils open source library.

I use it extensively in the Flextras Calendar and it works great. I'm pretty sure there is a DateAdd method. To get a date 3 months earlier, you can just add a negative 3.

http://flexdateutils.riaforge.org/




回答3:


I don't believe there is built-in Date arithmetic. Even the official adobe documentation for the Date class creates date math from scratch when using it.

Take a look at the above link. The documentation creates a DateMath class with static methods to do the sort of thing you want. Given that, I'm not sure why they didn't make is a part of the standard, but that's the way it is. I'd suggest copying it from there and expanding on it.




回答4:


another alternative is Peter's Dates for lazy people, I love it coming from CF http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/




回答5:


Use this:

var dObj:Date = new Date();

DateField_Now.formatString = DateField_LastWeek.formatString = "YYYY-MM-DD";

DateField_Now.selectedDate = dObj;

dObj["date"] += 7;
DateField_LastWeek.selectedDate = dObj;


来源:https://stackoverflow.com/questions/3106387/how-to-do-flex-date-deduction-and-addition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!