如何进行弹性日期扣除和加法 [英] How to do Flex date deduction and addition
问题描述
在 flex 中,我正在尝试进行日期推导和加法,但找不到方法.
In flex, I am trying to do date deduction and addition, but couldn't find a way to do it.
例如:public var dateNow:Date=new Date();
e.g.: public var dateNow:Date=new Date();
如何获得比 dateNow 早 3 个月的日期?
How can I get the Date 3 months earlier than dateNow?
谢谢!!!
推荐答案
您可以为此使用 Date 构造函数.Date 构造函数的第一个参数需要年份或时间戳.您可以使用 Date.time
属性从日期对象中获取时间戳.获得时间戳后,您可以从中添加/减去一些秒数,然后将其传递给 new Date(timestamp)
,您将获得一个代表新时间戳的全新日期.
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.
编辑;正如评论者指出的那样,时间操纵可能不是最好的方法.但是您仍然可以使用 Date 构造函数,如下所示:
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);
Date 构造函数足够智能,可以处理负值或大于 11 的值.
The Date constructor is smart enough to deal with negative values, or values greater than 11.
这篇关于如何进行弹性日期扣除和加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!