angular - Angular2 date formatting and add one day -


i have date in yyyy-mm-dd format - defined this.querydate. set 2017-04-05

i show 2 buttons in system. first show date in format 5th april 2017. second button should following day.

actionsheet.addbutton({text: this.querydate });         actionsheet.addbutton({text: [this.querydate + 1 day] });  

i unsure how change date format , add 24 hours start date. new angular 2 how achieve using php. ugly angular2 + php mongrel code ..

actionsheet.addbutton({text: date("js m y", strtotime(this.querydate)) });                actionsheet.addbutton({text: date("js m y", strtotime(this.querydate)+(60*60*24)) });                

how can achieve in angular 2?

your code pretty similar how works in javascript.

new date((new date(this.querydate)).gettime() + (60*60*24*1000)); 

and combine code:

actionsheet.addbutton({text: new date((new date(this.querydate)).gettime() + (60*60*24*1000)) });  

to explain, new date('1990 04 03') parses date string new date variable. .gettime() gets time timestamp (number of ms midnight jan 1, 1980 (i think). add 1 day, in milliseconds. finally, new date(...); parses number date.

you might notice date doesn't parse correctly, if it's in format yyyy-mm-dd. when tried out, several hours off time expected be, because it's giving me time in gmt, not timezone, if gave date in format yyyy mm dd, in timezone. default date parsing tools aren't best in js, unfortunately.

if want use specific timezone, recommend using moment.js parse date, since it's more robust.

you can learn more js date parsing here.

edit

i forgot mention, there's no easy way output date in js in specific format, may have craft using getdate(), getmonth(), , getfullyear(). if choose use moment.js, have functionality easier date formatting.


Comments