日期字段范围 - Flex

发布于 2024-12-07 12:56:04 字数 1312 浏览 3 评论 0原文

我有两个日期字段。有没有一种方法可以将第二个日期字段的日期设置为指定范围(比如说三天......?我尝试这样做但没有成功......不知道将日期转换回字符串并且格式化它..这是代码

<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100"  x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2"  parseFunction="null" width="100"  x="426" y="130"/>

private function leaveDate():void {
    //var dateLeave:Date = dfield1.selectedDate;
    var myTime:Date =dfield1.selectedDate;
    dateAdd("date", 3, myTime);
    //car formatter1:Datef
    commentField.text+=myTime+"\n"
    //dfield2.selectedDate = returnDate
}
public static function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
        {
    if (date == null) {
        date = new Date();
    }

    var returnDate:Date = new Date(date.time);;

    switch (datepart.toLowerCase()) {
        case "fullyear":
        case "month":
        case "date":
        case "hours":
        case "minutes":
        case "seconds":
        case "milliseconds":
            returnDate[datepart] += number;
            break;
        default:
            /* Unknown date part, do nothing. */
            break;
    }
      return returnDate;
}

如果有人可以提供帮助,我将非常感激

I have two Datefields .Is there a way that i can set the date of second Datefield to a specified range(say three days ...?. I tried to do it but not worked .. dont know to convert date back to string and format it .. here is the code

<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100"  x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2"  parseFunction="null" width="100"  x="426" y="130"/>

private function leaveDate():void {
    //var dateLeave:Date = dfield1.selectedDate;
    var myTime:Date =dfield1.selectedDate;
    dateAdd("date", 3, myTime);
    //car formatter1:Datef
    commentField.text+=myTime+"\n"
    //dfield2.selectedDate = returnDate
}
public static function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
        {
    if (date == null) {
        date = new Date();
    }

    var returnDate:Date = new Date(date.time);;

    switch (datepart.toLowerCase()) {
        case "fullyear":
        case "month":
        case "date":
        case "hours":
        case "minutes":
        case "seconds":
        case "milliseconds":
            returnDate[datepart] += number;
            break;
        default:
            /* Unknown date part, do nothing. */
            break;
    }
      return returnDate;
}

It would be really grateful if somebody can help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-12-14 12:56:04

要对日期进行计算,您必须首先将它们转换为数字,即大纪元时间中的日期或自 1970 年 1 月 1 日以来经过的秒数。然后,您可以对日期执行任何操作。例如 :

// Today
var today:Date = new Date();

// Tomorrow
var tomorrow:Date = new Date();

// How many seconds in a day ?
var secsInOneDay:Number = 60*60*24;

// Changes the tomorrow date to actually be tomorrow ^^
tomorrow.setTime(today.getTime() + secsInOneDay);

To operate calculations on date, you must first convert them into numbers, i.e. dates in Epoch time or amount of seconds elapsed since january the first in 1970. Then, you can performa any operation on your date. For instance :

// Today
var today:Date = new Date();

// Tomorrow
var tomorrow:Date = new Date();

// How many seconds in a day ?
var secsInOneDay:Number = 60*60*24;

// Changes the tomorrow date to actually be tomorrow ^^
tomorrow.setTime(today.getTime() + secsInOneDay);
一袭白衣梦中忆 2024-12-14 12:56:04

如果您想禁用 dfield1 中所选日期之前的日期以及 dfield1 中所选日期三天后的日期,请尝试如下操作:

<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100"  x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2"  parseFunction="null" width="100"  x="426" y="130"/>

private function leaveDate():void {
   dfield2.disabledRanges = [ { rangeEnd:new Date(
   dfield1.selectedDate.fullYear,
   dfield1.selectedDate.month,
   dfield1.selectedDate.date - 1 ) },
   { rangeStart:new Date(
   dfield1.selectedDate.fullYear,
   dfield1.selectedDate.month,
   dfield1.selectedDate.date + 3 ) } ];
}

If you want to disable the dates prior to the date selected in dfield1 and the dates after three days after the date selected in dfield1 then try something like this:

<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100"  x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2"  parseFunction="null" width="100"  x="426" y="130"/>

private function leaveDate():void {
   dfield2.disabledRanges = [ { rangeEnd:new Date(
   dfield1.selectedDate.fullYear,
   dfield1.selectedDate.month,
   dfield1.selectedDate.date - 1 ) },
   { rangeStart:new Date(
   dfield1.selectedDate.fullYear,
   dfield1.selectedDate.month,
   dfield1.selectedDate.date + 3 ) } ];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文