1

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.

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

2 Comments

worked because I used type="date", this works with type="datetime-local" this.meeting.start = today.toISOString().split('.')[0];
Ok it is displaying in the UI window, but the wrong (probably UTC) time
1

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;

Comments

0

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

8 Comments

Give me a sample this.meeting.start. It's giving me Date: "06/09/2018 20:49:23" for today's date, which is my local time. For you, you'll have to change the fmt to the one that I've updated in the answer.
Example: "Thu Sep 06 2018 10:00:00 GMT+0300". I also edited the post. The format is working but <input> is still not showing anything
Its formatting "Thu Sep 06 2018 10:00:00 GMT+0300" to "06/09/2018 10:00:00" but <input> has no value
I don't want input type to be text. it needs to be datetime-local
You've binded [(ngModel)] as well as value. When you should only be using one of them.
|
0

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 :

enter image description here

And the result is the browser itself is :

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.