格式化日期是JS

时间:2012-11-22 16:32:13

标签: javascript calendar

我正在尝试在Javascript中使用非常简单的东西。我需要脚本以下列格式打印出日期:yyyymmdd / yyyymmdd

我无法使用以下脚本。它会提供Google日历以输出当前日视图。

  var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();

    if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+''+mm+''+dd;



    document.write ('<iframe src="https://www.google.com/calendar/embed?

        showTitle=0&amp;dates=today"/"today&amp;mode=DAY&amp;height=1200&amp;wkst=1&amp;hl=en_GB&amp;bgcolor

=%23FFFFFF&amp;src=my cal source&amp;color=%23711616&amp;ctz=Etc%2FGMT" style=" border:solid 

1px #777 " width="950" height="715"frameborder="0" scrolling="no"></iframe>');

1 个答案:

答案 0 :(得分:4)

问题是字符串连接。你必须告诉JS什么是字符串以及要添加的变量是什么。更新您的代码如下:

document.write( '<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;dates='
    + today + '/' + today +
    '&amp;mode=DAY&amp;height=1200&amp;wkst=1&amp;hl=en_GB&amp;bgcolor=%23FFFFFF&src=my cal source&color=%23711616&ctz=Etc%2FGMT" style=" border:solid 1px #777 " width="950" height="715"frameborder="0" scrolling="no">' )
相关问题