Javascript 转换日期时间格式

Javascript 转换日期时间格式,javascript,jquery,datetime,Javascript,Jquery,Datetime,使用javascript转换日期时间格式的最佳方法是什么 我有这个。。“2015-08-06T00:39:08Z” 我想把它改成 “06/08/2015 12:39pm”您可以通过实例化日期对象手动执行此操作: var d = new Date('2015-08-06T00:39:08Z'); 然后使用Date方法(如getDay或getUTCFullYear)获取所需信息并构建格式化字符串 或者,如果您不介意依赖一个库,您可以使用它提供了许多方法,尤其是format方法: 您可以通过实例化日

使用javascript转换日期时间格式的最佳方法是什么

我有这个。。“2015-08-06T00:39:08Z” 我想把它改成


“06/08/2015 12:39pm”

您可以通过实例化日期对象手动执行此操作:

var d = new Date('2015-08-06T00:39:08Z');
然后使用Date方法(如
getDay
getUTCFullYear
)获取所需信息并构建格式化字符串

或者,如果您不介意依赖一个库,您可以使用它提供了许多方法,尤其是
format
方法:


您可以通过实例化日期对象手动执行此操作:

var d = new Date('2015-08-06T00:39:08Z');
然后使用Date方法(如
getDay
getUTCFullYear
)获取所需信息并构建格式化字符串

或者,如果您不介意依赖一个库,您可以使用它提供了许多方法,尤其是
format
方法:


您可以通过实例化日期对象手动执行此操作:

var d = new Date('2015-08-06T00:39:08Z');
然后使用Date方法(如
getDay
getUTCFullYear
)获取所需信息并构建格式化字符串

或者,如果您不介意依赖一个库,您可以使用它提供了许多方法,尤其是
format
方法:


您可以通过实例化日期对象手动执行此操作:

var d = new Date('2015-08-06T00:39:08Z');
然后使用Date方法(如
getDay
getUTCFullYear
)获取所需信息并构建格式化字符串

或者,如果您不介意依赖一个库,您可以使用它提供了许多方法,尤其是
format
方法:

使用newDate()方法,然后使用Date Get方法。示例以学校为例进行说明

方法描述

            getDate()   Get the day as a number (1-31)
            getDay()    Get the weekday as a number (0-6)
            getFullYear()   Get the four digit year (yyyy)
            getHours()  Get the hour (0-23)
            getMilliseconds()   Get the milliseconds (0-999)
            getMinutes()    Get the minutes (0-59)
            getMonth()  Get the month (0-11)
            getSeconds()    Get the seconds (0-59)
            getTime()   Get the time (milliseconds since January 1, 1970)
使用newDate()方法,然后使用Date Get方法。示例以学校为例进行说明

方法描述

            getDate()   Get the day as a number (1-31)
            getDay()    Get the weekday as a number (0-6)
            getFullYear()   Get the four digit year (yyyy)
            getHours()  Get the hour (0-23)
            getMilliseconds()   Get the milliseconds (0-999)
            getMinutes()    Get the minutes (0-59)
            getMonth()  Get the month (0-11)
            getSeconds()    Get the seconds (0-59)
            getTime()   Get the time (milliseconds since January 1, 1970)
使用newDate()方法,然后使用Date Get方法。示例以学校为例进行说明

方法描述

            getDate()   Get the day as a number (1-31)
            getDay()    Get the weekday as a number (0-6)
            getFullYear()   Get the four digit year (yyyy)
            getHours()  Get the hour (0-23)
            getMilliseconds()   Get the milliseconds (0-999)
            getMinutes()    Get the minutes (0-59)
            getMonth()  Get the month (0-11)
            getSeconds()    Get the seconds (0-59)
            getTime()   Get the time (milliseconds since January 1, 1970)
使用newDate()方法,然后使用Date Get方法。示例以学校为例进行说明

方法描述

            getDate()   Get the day as a number (1-31)
            getDay()    Get the weekday as a number (0-6)
            getFullYear()   Get the four digit year (yyyy)
            getHours()  Get the hour (0-23)
            getMilliseconds()   Get the milliseconds (0-999)
            getMinutes()    Get the minutes (0-59)
            getMonth()  Get the month (0-11)
            getSeconds()    Get the seconds (0-59)
            getTime()   Get the time (milliseconds since January 1, 1970)
使用纯javascript:

代码:

函数格式日期(输入){
var datePart=input.match(/\d+/g),
year=datePart[0],//仅获取两位数字
月份=日期部分[1],
日期=日期部分[2];
var h=parseInt(input.substr(input.indexOf(“T”)+1,2));
var m=parseInt(input.substr(input.indexOf(“:”)+1,2));
var dd=“AM”;
如果(h>=12){
h=hh-12;
dd=“PM”;
}
如果(h==0){
h=12;
}
m=m<10?“0”+m:m;
警报(日+'/'+月+'/'+年+'+h.toString()+':'+m.toString()+dd);
}
格式日期('2015-08-06T00:39:08Z');//"18/01/10"
使用纯javascript:

代码:

函数格式日期(输入){
var datePart=input.match(/\d+/g),
year=datePart[0],//仅获取两位数字
月份=日期部分[1],
日期=日期部分[2];
var h=parseInt(input.substr(input.indexOf(“T”)+1,2));
var m=parseInt(input.substr(input.indexOf(“:”)+1,2));
var dd=“AM”;
如果(h>=12){
h=hh-12;
dd=“PM”;
}
如果(h==0){
h=12;
}
m=m<10?“0”+m:m;
警报(日+'/'+月+'/'+年+'+h.toString()+':'+m.toString()+dd);
}
格式日期('2015-08-06T00:39:08Z');//"18/01/10"
使用纯javascript:

代码:

函数格式日期(输入){
var datePart=input.match(/\d+/g),
year=datePart[0],//仅获取两位数字
月份=日期部分[1],
日期=日期部分[2];
var h=parseInt(input.substr(input.indexOf(“T”)+1,2));
var m=parseInt(input.substr(input.indexOf(“:”)+1,2));
var dd=“AM”;
如果(h>=12){
h=hh-12;
dd=“PM”;
}
如果(h==0){
h=12;
}
m=m<10?“0”+m:m;
警报(日+'/'+月+'/'+年+'+h.toString()+':'+m.toString()+dd);
}
格式日期('2015-08-06T00:39:08Z');//"18/01/10"
使用纯javascript:

代码:

函数格式日期(输入){
var datePart=input.match(/\d+/g),
year=datePart[0],//仅获取两位数字
月份=日期部分[1],
日期=日期部分[2];
var h=parseInt(input.substr(input.indexOf(“T”)+1,2));
var m=parseInt(input.substr(input.indexOf(“:”)+1,2));
var dd=“AM”;
如果(h>=12){
h=hh-12;
dd=“PM”;
}
如果(h==0){
h=12;
}
m=m<10?“0”+m:m;
警报(日+'/'+月+'/'+年+'+h.toString()+':'+m.toString()+dd);
}
格式日期('2015-08-06T00:39:08Z');//"18/01/10"

第一个字符串是08--MM还是DD?你的答案应该是上午12:39而不是下午?第一个字符串是08--MM还是DD?你的答案应该是上午12:39而不是下午?第一个字符串是08--MM还是DD?你的答案应该是上午12:39而不是下午?我同意了moment.js。非常酷的图书馆。谢谢你的参考!我选择了moment.js。非常酷的图书馆。谢谢你的参考!我选择了moment.js。非常酷的图书馆。谢谢你的参考!我选择了moment.js。非常酷的图书馆。谢谢你的参考!