Convert string to date and add 5 days to it

前端 未结 2 1475
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 08:35

I have a string like so

\"2014-10-29\"

and Now I need to convert it to a date and add 5 days to it.

I have this code that adds 5 days t

2条回答
  •  遥遥无期
    2021-01-28 08:47

    Pass the string in to the Date constructor:

    var newDate = new Date("2014-10-29");
    newDate.setDate(newDate.getDate() + 5);
    
    var yyyy = newDate.getFullYear().toString();
    var mm = (newDate.getMonth() + 1).toString();
    var dd = newDate.getDate().toString();
    
    var mmChars = mm.split('');
    var ddChars = dd.split('');
    
    var newClosingDate = yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
    
    console.log(newDate);

提交回复
热议问题