Ionic framework 如何从ionic-2 calendar onchange()方法中获取所选日期

Ionic framework 如何从ionic-2 calendar onchange()方法中获取所选日期,ionic-framework,ionic2,ionic3,momentjs,angular-calendar,Ionic Framework,Ionic2,Ionic3,Momentjs,Angular Calendar,我使用的是ion2日历 这是我的html: <ion-calendar [(ngModel)]="date" (onChange)="onChange($event)" [type]="type" [format]="'YYYY-MM-DD'"> </ion-calendar> 这是我的控制台: onSelect event called 25-06-2018 但无论我选择哪一天,我

我使用的是ion2日历

这是我的html:

 <ion-calendar [(ngModel)]="date"
              (onChange)="onChange($event)"
              [type]="type"
              [format]="'YYYY-MM-DD'">
</ion-calendar>
这是我的控制台:

onSelect event called
25-06-2018
但无论我选择哪一天,我总是得到当前的月份和年份。只有日期值正在更改。 它在控制台中显示所有日期的当前月份和年份。 有人能告诉我如何从$event对象中以dd-mm-yy格式获取所选日期吗

编辑-这是我为console.log$event获得的结果

它显示了正确的点击日期。 如果有人能告诉我如何从中提取日期,那就太好了。

时刻语句总是像新日期一样生成当前时间

你应该使用像moment1995-12-25这样的语句或其他各种语句

详情请看这个

更新

先说结论,

.html没有任何更改

<ion-content padding>
  <ion-calendar [(ngModel)]="date"
  (onChange)="onChange($event)"
  [type]="type"
  [format]="'YYYY-MM-DD'">
</ion-calendar>
上面的代码为您提供了所需的内容

我想说的是,你们不应该使用瞬间,而应该使用瞬间。因为矩返回具有当前时间的矩实例,而矩事件返回具有时间事件承载的矩实例。

矩语句总是像新日期一样生成当前时间

你应该使用像moment1995-12-25这样的语句或其他各种语句

详情请看这个

更新

先说结论,

.html没有任何更改

<ion-content padding>
  <ion-calendar [(ngModel)]="date"
  (onChange)="onChange($event)"
  [type]="type"
  [format]="'YYYY-MM-DD'">
</ion-calendar>
上面的代码为您提供了所需的内容

我想说的是,你们不应该使用瞬间,而应该使用瞬间。因为矩返回具有当前时间的矩实例,而矩事件返回具有时间事件承载的矩实例。

$event of OnChange是矩对象

您可以这样格式化:$event.format'DD-MM-YYYY'

$event of OnChange是一个矩对象


您可以这样设置格式:$event.format'DD-MM-YYYY'

候选版本似乎将输出事件更改为change而不是onChange,并将select更改为onSelect。尝试使用以下方法:


`

候选版本似乎将输出事件更改为change而不是onChange,并将select而不是onSelect。尝试使用以下方法:


`

当您添加console.log$event时会发生什么情况{u isAMomentObject:true,{u i:1530210600000,{u isUTC:false,{…},{u locale:locale,}d:2018年6月29日星期五00:00:00 GMT+0530印度标准时间{u i:1530210600000 isAMomentObject:true{isUTC:false{isValid:true{locale:locale{…}日历:{…},日期格式:{…},{u invalidDate:Invalid date,序号:ƒ,{dayOfMonthOrdinalParse:/\d{1,2}th}st | nd | rd/,…}u pf:{empty:false,unusedTokens:Array0,unusedInput:Array0,溢出:-2,charsLeftOver:0,…}proto:Object@BerkAkkermancant您在onChange函数中使用this.date?console.log矩.format'DD-MM-YYYY';根本不访问this.date…您只是在创建一个新的日期object@SurajRaothis.date在日志中未定义。我想,这就是您想要的:console.log$event.format'DD-MM-YYYY'添加console.log$event时会发生什么在变更时刻{IsamentinObject:true,{i:1530210600000,{isUTC:false,{…},{…}地区:地区,}d:2018年6月29日星期五00:00:00 GMT+0530印度标准时间{}i:1530210600000{IsamentinObject:true{isUTC:false{有效:true{地区:地区{…}地区{日历:{…},{…}格式:{…},_invalidDate:Invalid date,序号:ƒ,_dayOfMonthOrdinalParse:/\d{1,2}th | st | nd | rd/,…}_pf:{empty:false,unusedTokens:Array0,unusedInput:Array0,溢出:-2,charsLeftOver:0,…}proto:Object@BerkAkkermancant您在onChange函数中使用this.date?console.log矩.format'DD-MM-YYYY';根本不访问this.date…您只是在创建一个新的日期object@SurajRao我想,这就是你想要的:console.log$event.format'DD-MM-YYYY'我不明白你的答案有什么关系到我的日历的onchange方法。我需要访问所选的日期。我不知道如何做,这是我的问题。我认为它仍然不太正确?事件已经是一个瞬间对象。你能把它再次传递到瞬间函数中吗?@SurajRao,你是对的。实际上,我们不需要一刻一刻地传递。我认为,reason@Nabil无法自己调试是因为对力矩用法的误解。所以,我只想指出。事件可以将自己的类型更改为[type],我可能会补充。我会在你的adivce上清楚地修改我的答案。我不明白你的答案与我的日历的onchange方法有什么关系。我需要访问所选的日期。我不知道怎么做,这是我的问题。我想它仍然不太正确。事件已经是一个矩对象。你能将它再次传递到矩函数中吗“调试?@SurajRao,你是对的。事实上,我们不需要一刻一刻地重复。在我看来,@Nabil无法自己调试的原因是因为误解。” 不利用时间。所以,我只想指出这一点。我可能会补充说,事件可以将自己的类型更改为[type]。我会在你同意后修改我的答案。
<ion-content padding>
  <ion-calendar [(ngModel)]="date"
  (onChange)="onChange($event)"
  [type]="type"
  [format]="'YYYY-MM-DD'">
</ion-calendar>
onChange(event) {
  console.log(event.format('DD-MM-YYYY')); // For actual usage.
  console.log(moment(event).format('DD-MM-YYYY')); // the statement you might think about
}
<ion-calendar [(ngModel)]="date" (change)="onChange($event)"  [type]="type" 
   [format]="'YYYY-MM-DD'">
</ion-calendar>
    
date;
type: "string";
onChange(event) {
    console.log(moment(this.date._d).format("YYYY-MM-DD"));
}