我需要jquery代码来转换日期格式而无需外部js库

时间:2016-04-27 06:40:37

标签: javascript jquery

我需要jquery / javascript代码来转换日期格式而不使用exteranl js库。以下是示例

我需要从FORMAT 1转换为FORMAT 2

FORMAT 1: Wed Apr 20 07:05:00 PDT 2016 
FORMAT 2: 2016-04-20 07:23:42.476

1 个答案:

答案 0 :(得分:0)

尝试:

var todayDate=new Date("Wed Apr 20 07:05:00 PDT 2016 ");
  var format ="AM";
  var hour=todayDate.getHours();
  var min=todayDate.getMinutes();
  var sec = todayDate.getSeconds();
  if(hour>11){format="PM";}
  if (hour   > 12) { hour = hour - 12; }
  if (hour   == 0) { hour = 12; }  
  if (min < 10){min = "0" + min;}
  document.write(todayDate.getFullYear() + "-" +(todayDate.getMonth()+1) + "-" + todayDate.getDate() + "  " +  hour+":"+min+":"+sec+" "+format);