【问题标题】:Flex add and subtract six months from now弹性加减从现在起六个月
【发布时间】:2011-11-28 16:25:55
【问题描述】:

这是我正在尝试的代码

        public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24
        var sixMonthsInMilis:int = 182 * millisecondsPerDay;
        var sixMonthsInTheFuture:Date = new Date();
        sixMonthsInTheFuture.setTime(sixMonthsInTheFuture.getTime() + sixMonthsInMilis );
        var sixMonthsInThePast:Date = new Date();
        sixMonthsInThePast.setTime(sixMonthsInThePast.getTime() - sixMonthsInMilis);

表示过去的一个是未来几天,另一个是过去几天。与我追求的 6 个月相差甚远

有什么想法吗?

【问题讨论】:

    标签: apache-flex


    【解决方案1】:
    var today:Date = new Date();
    today.setMonth(today.getMonth() + 6);
    var sixMonthsInTheFuture:Date = today;
    var anotherToday:Date = new Date();
    anotherToday.setMonth(anotherToday.getMonth() - 6);
    var sixMonthsInThePast:Date = anotherToday;
    
    trace(sixMonthsInTheFuture);
    trace(sixMonthsInThePast);
    

    对我来说,这返回

    2012 年 5 月 28 日星期一 14:03:32 GMT+0530
    2011 年 5 月 28 日星期六 14:03:32 GMT+0530

    这似乎很公平,对吧?

    【讨论】:

      【解决方案2】:

      嗯...变量

      var now:Date = new Date();
      now.setMonth(now.getMonth() + 6);
      var sixMonthsInTheFuture:Date = now;
      var now:Date = new Date();
      now.setMonth(now.getMonth() - 6);
      var sixMonthsInThePast:Date = now;
      

      不确定这是否会 100% 有效,因为我不确定 Flex 是否会溢出日期(例如,9 月 + 6 日会是明年的 1 月吗?),某些语言会。试试看

      显然 setMonth 还需要第二个参数 Day,它是 1 到 31 之间的天数,所以需要更多 ifs 来检查日期以及它是否超过新月份的最大天等,但这是一般的想法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        相关资源
        最近更新 更多