jsant甘特图-如何使用日期变量而不是文本

JSGantt Gantt Chart - How to use date variable instead of text

本文关键字:变量 文本 日期 何使用 jsant      更新时间:2023-09-26

我对Javascript比较陌生,一直在我的网站上使用JSGantt.js制作甘特图。html页面上唯一需要的代码如下所示:

<div style="position:relative" class="gantt" id="GanttChartDIV"></div>
<script>
    // here's all the html code neccessary to display the chart object
    // Future idea would be to allow XML file name to be passed in and chart tasks built from file.
    var g = new JSGantt.GanttChart('g',document.getElementById('GanttChartDIV'), 'quarter');
    g.setShowRes(0); // Show/Hide Responsible (0/1)
    g.setShowDur(0); // Show/Hide Duration (0/1)
    g.setShowComp(0); // Show/Hide % Complete(0/1)
    g.setCaptionType('None');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)
    g.setShowStartDate(0); // Show/Hide Start Date(0/1)
    g.setShowEndDate(0); // Show/Hide End Date(0/1)
    //var gr = new Graphics();
    if( g ) {
        // Parameters (pID, pName, pStart, pEnd,  pColor, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen, pDepend, pCaption)
        // You can also use the XML file parser JSGantt.parseXML('project.xml',g)
        g.AddTaskItem(new JSGantt.TaskItem(1, 'Title',     '', '',  '52A3CC', 'http://www.google.com', 0, 'Team Leader Name', 100, 1, 0, 1));
        g.Draw();   
        g.DrawDependencies();
    }
    else {
        alert("not defined");
    }
</script>

在g.d addtaskitem函数中,它期待输入到页面中的日期。我想在它的地方使用一个现有的日期变量。

我怎样才能做到这一点?谢谢你的帮助!如果您想查看提取这些信息的.js文件,请参见:

JS Gantt Download

假设它想要一个漂亮的ISO标准日期字符串,你可以这样做:

function getDate (days) {
     if (typeof days === "undefined") {
       var days = 0;
     }
     var date = new Date();
     return new Date(date.valueOf() + days*86400000).toISOString().substring(0,10);
}
<snip>
 g.AddTaskItem(new JSGantt.TaskItem(1, 'Title', getDate(-30), getDate(),  '52A3CC', 'http://www.google.com', 0, 'Team Leader Name', 100, 1, 0, 1));
<snip>

getDate([optional number])将返回今天的日期,根据您给出的天数进行调整-或者如果没有给出参数,则返回今天。理论上你可以做任何函数。如果这不是正确的日期格式,您可以使用toDateString()或其他日期函数代替toISOString().substring(0,10)。如果你愿意,你当然可以调整函数,甚至在运行时要求用户输入日期。