How to pass argument of Date to another function? My code:
var myDate = new Date(data.GetOPCResult.DateTime.match(/\d+/)[0] * 1);
var datlabel = document.getElementById("ct");
datlabel.innerHTML = GetTime(myD开发者_开发百科ate);
And GetTime function code:
function GetTime(DateTime) {
var month = (DateTime.getMonth() < 10) ? "0" + (DateTime.getMonth() + 1) : (DateTime.getMonth() + 1);
var day = (DateTime.getDate() < 10) ? "0" + DateTime.getMonth() : DateTime.getMonth();
var hour = (DateTime.getHours() < 10) ? "0" + DateTime.getHours() : DateTime.getHours();
var minute = (DateTime.getMinutes() < 10) ? "0" + DateTime.getMinutes() : DateTime.getMinutes();
var second = (DateTime.getSeconds() < 10) ? "0" + DateTime.getSeconds() : DateTime.getSeconds();
return DateTime.getDate() + "." + month + "." + DateTime.getFullYear() + " " + hour + ":" + minute + ":" + second;
}
This works for me
function GetTime(d) {
var month = (d.getMonth() < 10) ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1);
var day = (d.getDate() < 10) ? "0" + d.getMonth() : d.getMonth();
var hour = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
var minute = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
var second = (d.getSeconds() < 10) ? "0" + d.getSeconds() : d.getSeconds();
return d.getDate() + "." + month + "." + d.getFullYear() + " " + hour + ":" + minute + ":" + second;
}
alert(GetTime(new Date()));
Are you sure you are passing a valid Date object? Try passing new Date()
instead of myDate to your GetTime. If that works, your myDate variable is not a valid Date object.
Your code is fine. A little re-factoring will help though.
function GetTime(date) {
var day = zeroPad(date.getDate(), 2);
var month = zeroPad(date.getMonth() + 1, 2);
var year = zeroPad(date.getFullYear(), 4);
var hour = zeroPad(date.getHours(), 2);
var minute = zeroPad(date.getMinutes(), 2);
var second = zeroPad(date.getSeconds(), 2);
return day + "." + month + "." +
year + " " + hour + ":" +
minute + ":" + second;
}
function zeroPad(num, count) {
var z = num + '';
while (z.length < count) {
z = "0" + z;
}
return z;
}
Also please check what is data.GetOPCResult.DateTime
. I would say this will do.
var myDate = new Date( (data.GetOPCResult.DateTime || "")
.replace(/-/g,"/")
.replace(/[TZ]/g," ") );
精彩评论