使用年,月,日,小时,秒格式每分钟刷新图像

时间:2015-03-02 01:55:59

标签: javascript

<body>
<script language="javascript"> 

//Get Year, Month, Date, Hour and Minute to form Strings

    var y = today.getFullYear();
    var YY = y.toString();

    var m = today.getMonth()+1;
    if (m <= 9){
        m = '0' + m;
    }
    var MM = m.toString();

    var d = today.getDate();
    if (d <= 9){
        d = '0' +d;
    }
    var DD = d.toString();

    var h = today.getHours();
    if (h <= 9){
        h = '0' + h;
    }
    var HH = h.toString();

    var mi today.getMinutes();
    if (mi <=9){
        mi = '0' + mi;
    }
    var MiMi = mi.toString();

//Create image file in folder details, using year, month, date, hour and minute Strings
//Image file is in the /public_html folder, year+month+date sub-folder, hour sub-sub-folder,
// prefix with "tbmc_", year+month+date+hour+minutes in-between and ends with "59.jpg"  
// Assgin folder and image file details to a variable F 

    var F = "/public_html/"+YY+MM+DD+"/"+HH+"/tbmc_"+YY+MM+DD+"_"+HH+MiMi+"59.jpg";

//Try to print but nothing happen


<img scr= "F" width="1" height="1">

</script> 

</body>

2 个答案:

答案 0 :(得分:0)

您的javascript中存在错误: 我在开头添加了以下行:

var today = new Date();

并纠正了这一行:

    var mi = today.getMinutes();

缺少=。在这里小提琴:http://jsfiddle.net/robbyn/g1ut9n08/

//Get Year, Month, Date, Hour and Minute to form Strings
    var today = new Date();
    var y = today.getFullYear();
    var YY = y.toString();

    var m = today.getMonth()+1;
    if (m <= 9){
        m = '0' + m;
    }
    var MM = m.toString();

    var d = today.getDate();
    if (d <= 9){
        d = '0' +d;
    }
    var DD = d.toString();

    var h = today.getHours();
    if (h <= 9){
        h = '0' + h;
    }
    var HH = h.toString();

    var mi = today.getMinutes();
    if (mi <=9){
        mi = '0' + mi;
    }
    var MiMi = mi.toString();

//Create image file in folder details, using year, month, date, hour and minute Strings
//Image file is in the /public_html folder, year+month+date sub-folder, hour sub-sub-folder,
// prefix with "tbmc_", year+month+date+hour+minutes in-between and ends with "59.jpg"  
// Assgin folder and image file details to a variable F 

    var F = "/public_html/"+YY+MM+DD+"/"+HH+"/tbmc_"+YY+MM+DD+"_"+HH+MiMi+"59.jpg";

//Try to print but nothing happen

    alert(F);


        //Get Year, Month, Date, Hour and Minute to form Strings
        var today = new Date();
        var y = today.getFullYear();
        var YY = y.toString();

        var m = today.getMonth()+1;
        if (m <= 9){
            m = '0' + m;
        }
        var MM = m.toString();

        var d = today.getDate();
        if (d <= 9){
            d = '0' +d;
        }
        var DD = d.toString();

        var h = today.getHours();
        if (h <= 9){
            h = '0' + h;
        }
        var HH = h.toString();

        var mi = today.getMinutes();
        if (mi <=9){
            mi = '0' + mi;
        }
        var MiMi = mi.toString();

    //Create image file in folder details, using year, month, date, hour and minute Strings
    //Image file is in the /public_html folder, year+month+date sub-folder, hour sub-sub-folder,
    // prefix with "tbmc_", year+month+date+hour+minutes in-between and ends with "59.jpg"  
    // Assgin folder and image file details to a variable F 

        var F = "/public_html/"+YY+MM+DD+"/"+HH+"/tbmc_"+YY+MM+DD+"_"+HH+MiMi+"59.jpg";

    //Try to print but nothing happen

    alert(F);

答案 1 :(得分:0)

function appendZero(input) {
    return input.toString().length == 1 ? '0' + input : input;
}

//Get Year, Month, Date, Hour and Minute to form Strings
var today = new Date();

var YY = today.getFullYear();
var MM = appendZero(today.getMonth()+1);
var DD = appendZero(today.getDate());
var HH = appendZero(today.getHours());
var MiMi = appendZero(today.getMinutes());

//Create image file in folder details, using year, month, date, hour and minute Strings
//Image file is in the /public_html folder, year+month+date sub-folder, hour sub-sub-folder,
// prefix with "tbmc_", year+month+date+hour+minutes in-between and ends with "59.jpg"  
// Assgin folder and image file details to a variable F 

var F = "/public_html/"+YY+MM+DD+"/"+HH+"/tbmc_"+YY+MM+DD+"_"+HH+MiMi+"59.jpg";

//Try to print but nothing happen

alert(F);