javascript - Store every year directly from the date of entry of a user -


hi need store every year directly date of entry of user business problem every time add year inserted between day , month.

var yearoncompany = moment(user.fecha_ingreso_empresa, "yyyymmdd").fromnow(); var datestart = moment(user.fecha_ingreso_empresa).format("dd-mm-yyyy"); console.log(datestart); //03-12-2009 var f = parseint(yearoncompany); var yearsofwork = []; for(var = 1; <= f; i++) {     datestart = moment(datestart, "dd-mm-yyyy").add(1, 'years').calendar();     yearsofwork.push(datestart); } console.log(yearsofwork); 

result of this:

0:"12/03/2010" 1:"03/12/2011" 2:"12/03/2012" 3:"03/12/2013" 4:"12/03/2014" 5:"03/12/2015" 6:"12/03/2016" 7:"03/12/2017" 

this subtle error! according moment docs https://momentjs.com/docs/#/displaying/ .calendar() uses default date format, possibly using locale of environment, if none provided, here looks it's defaulting mm/dd/yyyy, rather desired dd/mm/yyyy. on first iteration datestart converted 12/03/2010 month , day swapped. on next iteration it's converted dd-mm-yyyy, again day , month swapped, when passed .calendar() again day , month swap again giving correct format again.

a potential bug you're changing datestart string moment object, i'd rewrite code this:

var datestart = moment(user.fecha_ingreso_empresa).format("dd-mm-yyyy"); var f = parseint(yearoncompany); var yearsofwork = []; for(var = 1; <= f; i++) {     datestart.add(1, 'years');     yearsofwork.push(datestart.format("dd-mm-yyyy")); } console.log(yearsofwork); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -