Format the following date into YYYY-mm-dd in JS

后端 未结 4 1190
礼貌的吻别
礼貌的吻别 2021-01-26 10:29

How would I go about converting following date:

Thu Feb 18 12:25:00 SGT 2016

into a format like \'2016-02-18\'?

I know,

  1. That, using a new
4条回答
  •  星月不相逢
    2021-01-26 11:25

    var input="Thu Feb 18 12:25:00 SGT 2016";
      
    var ar=input.split(" ");
    
      var months = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"};
    
    console.log(ar[ar.length-1]+"-"+months[ar[1]]+"-"+ar[2]);
    
      

    Try this code no date parsing required. Please check on console for result.

提交回复
热议问题