如何转换日期时间格式?

时间:2014-07-18 05:16:45

标签: javascript

如何将日期时间格式转换为 2012-02-22T11:34:00.000 + 0000

我想将datetime格式转换为这种格式,如下所示:

year-month-dayT10:45:00.000+0000

示例:

2012-02-22T11:34:00.000+0000

2 个答案:

答案 0 :(得分:0)

通过在此功能中传递一般日期,您可以执行此操作。

function getDateFormat(generalDate){
var sapDate = '0000-00-00T00:00:00';
   var date;
   if (!generalDate) {
       //NOP
   } else if(typeof generalDate == "string"){
    var dateArray = generalDate.split("-");
    if(dateArray.length < 2)
     dateArray = generalDate.split("/");
   date = new Date(monthNumber[dateArray[1]]+"/"+dateArray[0]+"/"+dateArray[2]);
   } else {
       date = new Date((generalDate.getTime() - (new Date().getTimezoneOffset() * 60000)) + 19800000);
   }

   if (date) {
       var monthIn =null;
       if(date.getMonth() < 9)
           monthIn = "0"+(date.getMonth()+1);
       else
           monthIn = date.getMonth()+1;

       var date2 = date.getDate();
       if(date2 <= 9)
           date2 = "0"+date2;

       sapDate = date.getFullYear()+'-'+monthIn+'-'+date2+'T00:00:00.000+0000';
   }

console.log(sapDate);
}

工作小提琴是here

答案 1 :(得分:0)

试试这个

<script>
var myDate = new Date();

var date = myDate.getDate();
var month = myDate.getMonth() + 1; 

var year = myDate.getFullYear();
var h = myDate.getHours();
var m = myDate.getMinutes();
var s = myDate.getSeconds();
var ms = myDate.getMilliseconds();
document.write(year + "-" + month + "-" + date + "T" + "-" + h + ":" + m + ":" + s + "." + ms);

</script>