最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular - HTML5 input type "datetime-local" not working with Date Object - Stack Overflow

programmeradmin0浏览0评论

I've tried in many ways. First I get the date as Moment.js object,

 this.meeting.start = event.date.local().toDate();

I also tried the following:

this.meeting.start = new Date(this.meeting.start.toLocaleString());

my html:

<input 
  type="datetime-local" 
  class="form-control" 
  value="{{meeting.start}}" 
  [(ngModel)]="meeting.start" />

but the UI window is empty:"--:-- dd/mm/yy"

Thanks.

I've tried in many ways. First I get the date as Moment.js object,

 this.meeting.start = event.date.local().toDate();

I also tried the following:

this.meeting.start = new Date(this.meeting.start.toLocaleString());

my html:

<input 
  type="datetime-local" 
  class="form-control" 
  value="{{meeting.start}}" 
  [(ngModel)]="meeting.start" />

but the UI window is empty:"--:-- dd/mm/yy"

Thanks.

Share Improve this question edited Sep 6, 2018 at 15:29 Aa Yy asked Sep 6, 2018 at 15:10 Aa YyAa Yy 1,7426 gold badges20 silver badges34 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

This works for me :

let today = new Date();
this.meeting.start = today.toISOString().split('T')[0]

in html

<input 
  type="date" 
  class="form-control" 
  value="{{meeting.start}}" 
  [(ngModel)]="meeting.start" />

please see example in https://angular-rb5vmu.stackblitz.io (last item : Input Date Format )

Edited!

Based on the help I got here, I managed to get this to work with this workaround:

var add = moment(this.meeting.start).add(3, 'hours');
var result = add.toISOString().split('.')[0];
this.meeting.start = result;

Try this

const fmt = 'HH:mm DD/MM/YY';
const result = moment.utc(this.meeting.start, fmt).local().format(fmt);

Template:

<input type="datetime-local" [value]="result">

Sample StackBlitz

This worked for me, I tried all the answers above but none worked..

// Init dates
let today = moment.utc().local().format('YYYY-MM-DDTHH:mm');
let todayMinus7Days = moment.utc().local().subtract(7, 'days').format('YYYY-MM-DDTHH:mm');

console.log(today);
console.log(todayMinus7Days);

And the result from the browser console is :

And the result is the browser itself is :

发布评论

评论列表(0)

  1. 暂无评论