Convert string to date and add 5 days to it

前端 未结 2 1501
伪装坚强ぢ
伪装坚强ぢ 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:44

    You could also use the wonderful library called moment.js - it makes working with dates in JavaScript an absolute breeze. Especially converting them back and forth to/from strings.

    With your date and using moment, you could do this for example:

    var stringFormat = 'YYYY-MM-DD', 
        date = moment('2014-10-29', 'YYYY-MM-DD');
    
    date.add(5, 'days');
    
    console.log(date.format(stringFormat);
    

    This will print out the string in the same format as you put in.

提交回复
热议问题