javascript 将日期参数传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7173521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:12:08  来源:igfitidea点击:

Passing Date argument to function

javascript

提问by amaranth

How to pass argument of Date to another function? My code:

如何将Date的参数传递给另一个函数?我的代码:

var myDate = new Date(data.GetOPCResult.DateTime.match(/\d+/)[0] * 1);
var datlabel = document.getElementById("ct");
datlabel.innerHTML = GetTime(myDate);

And GetTime function code:

和 GetTime 函数代码:

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;
}

采纳答案by Tobbe

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.

您确定要传递有效的 Date 对象吗?尝试将new Date()而不是 myDate传递给您的 GetTime。如果可行,则您的 myDate 变量不是有效的 Date 对象。

回答by naveen

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.

另请检查是什么data.GetOPCResult.DateTime。我会说这会做。

var myDate = new Date( (data.GetOPCResult.DateTime || "")
                .replace(/-/g,"/")
                .replace(/[TZ]/g," ") );