我正在使用Angular-UI时间插件,因此我需要将简单的时间格式转换为更复杂的时间格式,即
来自:
16:19:29
到:
Wed Dec 09 2015 16:09:15 GMT+0530 (India Standard Time)
或者如果可能的话,可以使用以下格式的完整数据:
"2015-12-17 16:19:29"
我从json收到的。
我需要知道存在任何将我们的简单时间格式转换为扩展形式的javascript函数(日期年份 不重要 ,我可以处理它!)< / p>
答案 0 :(得分:1)
试试这个简单的fiddle code
var timeStr = "16:19:29";
var timeStrArr = timeStr.split( ":" );
var date = new Date();
date.setHours( parseInt( timeStrArr[ 0 ] ) );
date.setMinutes( parseInt( timeStrArr[ 1 ] ) );
date.setSeconds( parseInt( timeStrArr[ 2 ] ) );
alert( date );
答案 1 :(得分:1)
试试这个演示
<html>
<head>
<title>Please Rate if it helps</title>
<script>
Date.prototype.myFormat = function (start) {
var temporeryDay = this.getDate();
var temporeryMonth = this.getMonth() + 1;
var temporeryYear = this.getFullYear();
var temporeryHours = this.getHours();
var temporeryMinutes = this.getMinutes();
var temporerySeconds = this.getSeconds();
temporeryDay = temporeryDay.toString().length == 1 ? "0" + temporeryDay.toString() : temporeryDay.toString();
temporeryMonth = temporeryMonth.toString().length == 1 ? "0" + temporeryMonth.toString() : temporeryMonth.toString();
temporeryHours = temporeryHours.toString().length == 1 ? "0" + temporeryHours.toString() : temporeryHours.toString();
temporeryMinutes = temporeryMinutes.toString().length == 1 ? "0" + temporeryMinutes.toString() : temporeryMinutes.toString();
temporeryHours = temporeryHours.toString().length == 1 ? "0" + temporeryHours.toString() : temporeryHours.toString();
temporerySeconds = temporerySeconds.toString().length == 1 ? "0" + temporerySeconds.toString() : temporerySeconds.toString();
return (temporeryYear + "-" + temporeryMonth + "-" + temporeryDay + " " + temporeryHours + ":" + temporeryMinutes + ":" + temporerySeconds);
}
window.onload = function () {
document.write(new Date().myFormat());
}
</script>
</head>
<body>
</body>
</html>