javascript日期转换

时间:2011-11-07 14:26:39

标签: javascript date

我想获取现有的javascript字符串并将其转换为javascript timestamp对象, 然后存储为格式YYYYMMDDHHMMSS

mystring = "Mon Nov 07 2011 09:20:58 GMT-0500 (Eastern Standard Time)";
var timeObject = new Date(mystring); 

1 个答案:

答案 0 :(得分:0)

// pass in javascript date object, return variations of YYYY-MM-DD HH:MM:SS 
function fnFormatDateTime(timeObject)
{
    // construct day/month/year
    var month = timeObject.getMonth() + 1;
    if (month < 10){
        month = "0" + month;
    }               
    var day = timeObject.getDate();
    if (day < 10){
        day = "0" + day;
    }               
    var year = timeObject.getFullYear();    
    var currentDate  = (" " + year +  month + day).trim();
    var currentDateFormatted  = ( year +"-"+  month +"-"+ day);

    // construct current time
    var hours = timeObject.getHours();
    if (hours < 10){
        hours = "0" + hours;
    }   
    var minutes = timeObject.getMinutes();
    if (minutes < 10){
        minutes = "0" + minutes;
    }
    var seconds = timeObject.getSeconds();
    if (seconds < 10){
        seconds = "0" + seconds;
    }               
    var currentTime  = (hours +  minutes + seconds + " ").trim();
    var currentTimeFormatted  = (hours +":"+ minutes +":"+ seconds);

     var retArray = new Array();     
        retArray[0] = (currentDateFormatted + " " + currentTimeFormatted);      
        retArray[1] = (currentDate+ " " + currentTime);             
        retArray[2] = currentDate;      
        retArray[3] = currentTime;
    return retArray;    
}