Format a date string in javascript

后端 未结 7 1530
误落风尘
误落风尘 2020-12-03 15:26

Hello every i have date field of type string with iso format like this: const date = \"2017-06-10T16:08:00: i want somehow to edit the string in the following format like th

7条回答
  •  温柔的废话
    2020-12-03 16:18

    It can be achieved without moment.js, but I suggest you use it

    var date = new Date("2017-06-10T16:08:00");
    
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    
    if (day < 10) {
      day = '0' + day;
    }
    if (month < 10) {
      month = '0' + month;
    }
    
    var formattedDate = day + '-' + month + '-' + year
    

提交回复
热议问题