DateTime - default to todays date


#12

I have the same problem. My date is always showed with a day less. I tried moment (http://momentjs.com/guides/), pipe “date: “dd/MM/yyyy””, but didn´t work. It is funny, because, the value in data base is correct, but when I try to format the date, a date with a less day is showed.


#13

yes, i still didn’t solve this issue. i don’t know what to do…


#14

I solve this:

In a .ts file:

  getDate(datepar){

     var dateParts = datepar.split("-");
     var date = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
     return date;
  }

In the .html
<p>{{getDate(datepar)}}</p>

You can see more details about the this problem here http://stackoverflow.com/questions/7556591/javascript-date-object-always-one-day-off

Good luck!


#15

thank u,
what is datepar in your project? date selected?


#16

datepar = date parameter… Just used for the example.


#17

To get the ISO string and set the timezone, you can use this function (taken from this Stack Overflow post)

var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1);

Ion-datetime placeholder not working
#18

Anyone know how to get the hour and minute separately?
I have this:

<ion-datetime
    PickerFormat = "HH: mm"
    [(NgModel)] = "time"
    CancelText = "Cancel"
    DoneText = "OK">
</ion-datetime>

Which saves a string in my variable time when selecting a schedule. I need to get hours and minutes apart. Can not it be that instead of saving a string, I keep a Date object or something like that?


#19

+1. Would like to have a property to set default picker date when it slides up or at least to be the current date\time.
I also have trouble as @sdrew, I’m using reactive forms and have optional dates and don’t want to make them required.


#20

I want to customize date in ionic 2.
I want to add 3 days in today’s date

For Example if today is 5 August it should display 8 august.

How i can customize it ?


#21

date-fns/add_days.


#22

@rapropos this date-fns not working on my side after installing “npm install date-fns --save”.

I am using typescript in ionic 2.

Do you know why it is not adopting this library ?


#23

No, because it works for me, so you would have to share your code for anybody to be able to say anything constructive.


#24

@rapropos

in .ts file i am getting date like this
myDate: String = new Date().toISOString();

and in .html file
" ion-datetime displayFormat=“YYYY/MM/DD” pickerFormat=“YYYY MMMM DDDD” min=“2017” [(ngModel)]=“myDate” name=“booking_date”></ion-datetime "


#25

I am not seeing anywhere in what you posted where you call anything from date-fns.


#26

@rapropos
i found out the solution

new Date(new Date().getTime()+(32460601000)).toISOString();
in this I am adding 3 days in the actual date


#27

OK, whatever. The entire point of libraries like date-fns is to eliminate the need for such ugliness as (3*24*60*60*1000), but if you’re not sufficiently motivated to get it to work and are happy with that, it’s not my business.


#28

new Date().toISOString() is returning wrong time. It does not calculate the timezone I suppose.
Any idea how to fix this.
I am trying on simulator and time in simulator is 11:40 AM but Date object returns 9:40 AM


Calendar for scheduling app
#29

Javascript time library does not manage time correctly.
I recommend using moment.js.
go to you folder application, run : npm install moment --save
in your page.ts add: import * as moment from ‘moment’;
declare your variable: myDate= moment().format();

Now just use ion-datetime like this:

                <ion-datetime type="datetime-local" displayFormat="MMM, YYYY" max="2099-12-31" [(ngModel)]="myDate"  ></ion-datetime>

I hope this helps.


#30

This answer is spot on


#31

Hello, your solution helped me. Would you like to know if you can put this result inside an object? Has an obejto receiving date:

Test = {data: ‘’, value: ‘’};

I tried to put

Test = {data: moment (). Format (), value: ‘’};

but it does not work