这是一个非常简单的问题。在div标签内部有一个描述,它包含一个日期时间(格式为yyyy-mm-dd hh:mm:ss)。我使用正则表达式捕获此日期时间,然后转换为日期。
假设我有"现在2015-01-02 11:47:50一些陈述....."。毕竟,我希望得到" 2015-01-02 11:47:50"仅部分。但是现在我收到它" Fri Jan 02 2015 03:47:50 GMT-0800(太平洋标准时间)"。
我哪里错了?任何人都可以给我一个建议,得到结果为yyyy-mm-dd hh:mm:ss格式?
谢谢..
(这里我附上了我试过的代码)
var getDiv = document.getElementById('aasd');
var aa = getDiv.innerHTML;
var result = dateCatcher(aa);
alert(dateCatcher(aa));
//Gives the date part from the whole statement
function dateCatcher(statement){
var date_finder =/(\d{4})\-(\d{2})\-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/;
var datePart = statement.match(date_finder);
datePart[2] -= 1;
var UtcDate = new Date(Date.UTC.apply(this, datePart.slice(1)));
return UtcDate;
}

<html>
<body>
<div id = "aasd"> Hi, Now 2015-01-02 11:47:50 ddfd dfdsfdf dff </div>
</body>
</html>
&#13;
答案 0 :(得分:0)
试试这个 使用dateformatter.js 如下面给出的功能
Date.prototype.customFormat = function(formatString){
var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th;
var dateObject = this;
YY = ((YYYY=dateObject.getFullYear())+"").slice(-2);
MM = (M=dateObject.getMonth()+1)<10?('0'+M):M;
MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substring(0,3);
DD = (D=dateObject.getDate())<10?('0'+D):D;
DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][dateObject.getDay()]).substring(0,3);
th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
h=(hhh=dateObject.getHours());
if (h==0) h=24;
if (h>12) h-=12;
hh = h<10?('0'+h):h;
AMPM=(ampm=hhh<12?'am':'pm').toUpperCase();
mm=(m=dateObject.getMinutes())<10?('0'+m):m;
ss=(s=dateObject.getSeconds())<10?('0'+s):s;
return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm).replace("#AMPM#",AMPM);
}
function formatInputDate(inputDate, dateFormat) {
if(inputDate) {
var dateToFormat = new Date(inputDate);
return dateToFormat.customFormat(dateFormat);
}
}
并调用
console.log(formatInputDate(yourDate,'#YYYY #MMM# #DD# '));
答案 1 :(得分:0)
我建议将此作为解决方案:
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var hours = dateObj.getUTCHours();
var minutes = dateObj.getUTCMinutes();
var seconds = dateObj.getUTCSeconds();
var newdate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
$('#time').append('Time is: ' + newdate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="time"></p>
你也可以检查一下。它可能会有所帮助: 完整的日期对象功能列表: DATE
getMonth()
根据当地时间返回指定日期的月份(0-11)。
getUTCMonth()
根据通用时间返回指定日期的月份(0-11)。
答案 2 :(得分:0)
以下代码将执行此操作,此外它将添加&#34; 0&#34;如果dd和mm < 10。
function getUTCTime(d){
utcTime = new Date(d)
var dd = utcTime.getDate();
var mm = utcTime.getMonth()+1;
var yyyy = utcTime.getFullYear();
var hh = utcTime.getHours();
var min = utcTime.getMinutes();
var ss = utcTime.getSeconds();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + ss;
}