2014-01-17 59 views

回答

0

您可以使用下面的代码。 用结束日期编辑名为targetDate的变量。

function doGet() { 
    var today = new Date(); 
    var targetDate = new Date(2014, 1, 25) // - February 25th of 2014 
    today.setHours(0,0,0,0); 

    var ui = UiApp.createApplication(); 
    ui.add(ui.createLabel(calcDate(today, targetDate))); 
    return ui; 
} 


function calcDate(date1,date2) { 
    var diff = Math.floor(date2.getTime() - date1.getTime()); 
    var day = 1000* 60 * 60 * 24; 

    var days = Math.floor(diff/day); 
    var months = Math.floor(days/31); 
    var years = Math.floor(months/12); 

    var message = days + " days " ; 
    message += months + " months "; 
    message += years + " years left \n"; 

    return message 
} 

现场版可以找到here

+0

此代码是开始。如果将其嵌入到侧边栏并显示实时倒计时,可以对gdoc进行改进。为此,您需要使用htmlService(使用setInterval)而不是像这样完成的uiservice。 –

0

只是要注意;我盲目地改变了制定目标日期的数量,但要小心,从0开始的月数不是1. 0是1月,1月是2月等。