首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使输入类型时间与TypeScript和Angular2一起工作?

使输入类型时间与TypeScript和Angular2一起工作?
EN

Stack Overflow用户
提问于 2017-05-02 16:07:09
回答 1查看 5K关注 0票数 2

在我的EventCreate的TypeScript类中,我有带有数据类型DatestartDateTimeendDateTime属性。HTML5使用输入类型time来获取时间。我只想问一问:如何使输入类型time与TypeScript和Angular2一起工作?

代码语言:javascript
复制
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { EventCreate } from './eventCreate';


@Component({
    selector: 'add-Event',
    templateUrl: './event-add.component.html',
})

export class EventAddComponent {
    title: string;
    description: string;
    locationId: number;
    locationDetails: string;
    categoryId: number;
    isRegistrationRequired: boolean;
    eventDate: Date;
    startDateTime: Date;
    endDateTime: Date;
    maximumRegistrants: number;

    newEvent: EventCreate;

    constructor(private router: Router) { }

    createNewEvent() {
        console.log('new Event Created');
        this.newEvent = new EventCreate(
            this.title,
            this.description,
            this.locationId,
            this.locationDetails,
            this.categoryId,
            this.isRegistrationRequired,
            this.eventDate,
            this.startDateTime,
            this.endDateTime,
            this.maximumRegistrants
        );
        console.log(this.newEvent);
        //call service
        //call route to go to Home page 
        this.router.navigate(['/home']);
    }
    cancel() {
        this.router.navigate(['/home']);
    }
}
    export class EventCreate {
        constructor(
            public title: string,
            public description: string,
            public locationId: number,
            public locationDetails: string,
            public categoryId: number,
            public isRegistrationRequired: boolean,
            public eventDate: Date,
            public startDateTime: Date,
            public endDateTime: Date,
            public maximumRegistrants: number,
        ) { }
    }

<form>
    <div class="form-group">
        <label>Start Time:</label>
        <input type="time" name="startDateTime" [(ngModel)]="startDateTime">
    </div>
    <div class="form-group">
        <label>End Time:</label>
        <input type="time" name="endDateTime" [(ngModel)]="endDateTime">
    </div>
</form>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 20:17:23

我知道现在有点晚了,但不管怎样都能帮上忙。输入类型" time“输出输入时间的字符串表示形式。如果您的目标是从这个"time“字符串中获取一个类型记录日期对象(出于某些原因),我建议您查看输入类型日期时间-本地,然后将其值转换为类型记录日期对象为new Date(your_input_value)。下面是一个示例代码(我使用的是反应形式和角度材料):

代码语言:javascript
复制
<form [formGroup]="meetingForm" novalidate autocomplete="off">
.............. other input fields .....................
<input matInput 
type="datetime-local" 
placeholder="Start Time" 
formControlName="meetingStartTime">
....................................
</form>

然后组件类的一部分看起来类似于

代码语言:javascript
复制
.........other codes................
constructor(formBuilder:FormBuilder, ...){}
...............................
let meetingStartTimeFormControl = this.meetingForm.get('meetingStartTime') as FormControl;
this.startDateTime = new Date(this.meetingStartTimeFormControl.value);
console.log(this.startDateTime)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43742531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档