pelu
pelu

Reputation: 93

Display Today, yesterday, tommorrow instead of dates in ionic

i am making an ionic app v2 in which i have to show 'today' instead of current date and 'yesterday' and 'tomorrow' also for respective dates. i tried using moment but it gives days till last week like last monday and reference time issue is also there with moment. i need for only these 3 days without reference time . can you tell me how to customize moment in ionic framework ? if you have any other suggestions than using moment . please tell. thanks in advance. P.S: i want to implement this only in html code of ionic not in typescript code.

Upvotes: 1

Views: 2749

Answers (2)

BRass
BRass

Reputation: 3838

Agreed that this will be difficult without JS/TS. In your .ts file couldn't you have 3 date member variables:

//Set up 3 new dates, defaulting them to today
yesterday: Date = new Date();
today: Date = new Date();
tomorrow: Date = new Date();

And then in your ctor or init method, set them up properly (below is likely not the most valid/efficient way, but is an example).

//Today is already set up from instantiation, but re-set tomorrow and yesterday    
this.tomorrow.setDate(this.tomorrow.getDate() + 1);
this.yesterday.setDate(this.yesterday.getDate() - 1);

And then in your HTML, bind to them:

Yesterday was: {{yesterday?.toDateString()?.slice(0,10)}}
<br> Today is: {{today?.toDateString()?.slice(0,10)}}
<br> Tomorrow will be: {{tomorrow?.toDateString()?.slice(0,10)}}

Upvotes: 0

Imdad Ali
Imdad Ali

Reputation: 737

Just like this: moment().add(-1, 'days'). It will give you the previous day with the same exact current time that is on your local pc.

Ref here

Upvotes: 0

Related Questions