Javascript 从字符串格式化日期时间

Javascript 从字符串格式化日期时间,javascript,jquery,html,Javascript,Jquery,Html,我有JavaScript格式的日期: 2012年1月27日星期五08:01:00 GMT+0530(印度标准时间) 我想将日期格式化为MM/dd/yyyy HH:MM:ss只需将其连接为如下字符串: var date = new Date(); var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" + (date.getMonth()+1); var day = (date.getDate()+1) > 9 ?

我有JavaScript格式的日期:

2012年1月27日星期五08:01:00 GMT+0530(印度标准时间)


我想将日期格式化为
MM/dd/yyyy HH:MM:ss

只需将其连接为如下字符串:

var date = new Date();
var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" + (date.getMonth()+1);
var day = (date.getDate()+1) > 9 ? (date.getDate()+1) : "0" + (date.getDate()+1);
var hours = (date.getHours()) > 9 ? (date.getHours()) : "0" + (date.getHours());
var minutes = (date.getMinutes()) > 9 ? (date.getMinutes()) : "0" + (date.getMinutes());
var seconds = (date.getSeconds()) > 9 ? (date.getSeconds()) : "0" + (date.getSeconds());

var dateString = 
    month + "/" + 
    day + "/" + 
    date.getFullYear() + " " + 
    hours + ":" + 
    minutes + ":" + 
    seconds;
dd/mm/yyyy HH:mm:ss
请记住,javascript日期的月份是以零为基础的

方法
.toLocalString
返回类似的结果,但月份和日期切换如下:

var date = new Date();
var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" + (date.getMonth()+1);
var day = (date.getDate()+1) > 9 ? (date.getDate()+1) : "0" + (date.getDate()+1);
var hours = (date.getHours()) > 9 ? (date.getHours()) : "0" + (date.getHours());
var minutes = (date.getMinutes()) > 9 ? (date.getMinutes()) : "0" + (date.getMinutes());
var seconds = (date.getSeconds()) > 9 ? (date.getSeconds()) : "0" + (date.getSeconds());

var dateString = 
    month + "/" + 
    day + "/" + 
    date.getFullYear() + " " + 
    hours + ":" + 
    minutes + ":" + 
    seconds;
dd/mm/yyyy HH:mm:ss

这里有一个

试试这样的方法

        var date = new Date();
        var month = date.getMonth()+ 1;
        var dateString =  month + "/" + date.getDate() + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
        console.log(dateString);
为此:

//Formats d to MM/dd/yyyy HH:mm:ss format
function formatDate(d){
  function addZero(n){
     return n < 10 ? '0' + n : '' + n;
  }

    return addZero(d.getMonth()+1)+"/"+ addZero(d.getDate()) + "/" + d.getFullYear() + " " + 
           addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getMinutes());
}

var str = 'Fri Jan 27 2012 08:01:00 GMT+0530 (India Standard Time)';
var date = new Date(Date.parse(str));
var formatted = formatDate(date);
alert(formatted);  //alerts "01/26/2012 22:31:31"
//格式d到MM/dd/yyyy HH:MM:ss格式
函数格式化日期(d){
函数addZero(n){
返回n<10?'0'+n:''+n;
}
返回addZero(d.getMonth()+1)+“/”+addZero(d.getDate())+“/”+d.getFullYear()+”+
addZero(d.getHours())+“:”+addZero(d.getMinutes())+“:”+addZero(d.getMinutes());
}
var str=‘2012年1月27日星期五08:01:00 GMT+0530(印度标准时间)’;
var date=新日期(date.parse(str));
var formatted=formattate(日期);
警报(格式化)//警报“01/26/2012 22:31:31”
Cheers是datetimeformat的插件

  $(document).ready(function () {
        console.log($.format.date("Fri Jan 27 2012 08:01:00 GMT+0530 (India Standard Time)", "MM/dd/yyyy HH:mm:ss"));
    });

。这里需要注意的是,JS中的月份是以零为基础的,因此
date.getMonth()+1
。是的,我要编辑答案;)这是唯一的方法吗?任何预定义函数都是唯一的函数
。toLocaleString
,但您需要月首,对吗?您可以使用像矩.js这样的库,它包含一个自定义格式函数。