如何使用JavaScript中的提示显示日期?

时间:2014-03-12 06:49:36

标签: javascript prompt

我正在尝试使用JavaScript在提示框中插入日期。我该怎么做? 我尝试使用两个提示窗口分别使用提示窗口插入月份和日期。

1 个答案:

答案 0 :(得分:0)

JavaScript prompt method接受字符串作为输入。您可以使用日期对象,执行一些字符串格式化并将其传递给提示对象,它应该如下所示:

Demo

<强> JavaScript的:

var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];
function myFunction() {
    var today = new Date();
    var date = prompt("Please enter date.", today.getDate()+"-"+monthNames[today.getMonth()]+"-"+today.getFullYear());

    if (date != null) {
        x = "Hello! You have entered date as: " + date;
        document.getElementById("demo").innerHTML = x;
    }
}