如何使用javascript为selenium IDE获取明天的日期?

时间:2012-07-23 06:33:56

标签: javascript selenium

我需要明天约会并需要存储在selenium IDE中。

任何人都可以帮忙吗。

4 个答案:

答案 0 :(得分:2)

你可以像这样得到明天的日期:

var d = new Date();
d.setDate(d.getDate() + 1)
console.log(d.toLocaleDateString();) //or d, depends on how you want that date

并将其存储在Selenium中,也许this可以帮助您

答案 1 :(得分:0)

对于上述问题,我已经在codebins上完成了bin解决方案。演示链接如下:

演示: http://codebins.com/bin/4ldqp9v

请通过添加评论,告诉我此解决方案是否对您有所帮助。

HTML:

<div id="panel">
  <input type="text" size="15" id="txtdate" name="txtdate" />
  <input type="button" id="btndate" name="btndate" value="Show Tommorrow Date" />
  <div id="result">
  </div>
</div>

<强>使用Javascript:

var dateBtn = document.getElementById('btndate');

dateBtn.onclick = function() {
    var strdate = document.getElementById('txtdate').value;
    if (strdate != "" && strdate != null && typeof(strdate) != "undefined") {

        if (isValidDate(strdate)) {

            var userdate = new Date(strdate);
            var newdate = new Date(userdate.getTime() + 1 * 24 * 60 * 60 * 1000);

            document.getElementById("result").innerHTML = "Tomorrow date is: " + (newdate.getMonth() + 1) + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " [MM/dd/YYYY]";
        }

    } else {
        alert("Invalid date format value..!");
        return false;
    }
}

//Function for checking valid date format/value.
function isValidDate(dateStr) {

    // Checks for the following valid date formats:
    // MM/DD/YYYY
    // Also separates date into month, day, and year variables
    var datePat = /^(\d{2,2})(\/)(\d{2,2})\2(\d{4}|\d{4})$/;

    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null) {
        alert("Date must be in MM/DD/YYYY format")
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12");
        return false;
    }
    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31");
        return false;
    }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        alert("Month " + month + " doesn't have 31 days!")
        return false;
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

演示: http://codebins.com/bin/4ldqp9v

答案 2 :(得分:0)

Selenium.prototype.doGenerateTomorrowDate = function( varName1, varName2 ) 
{
    var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    var month = monthNames[month];
    storedVars[ varName1 ] =  day;
    storedVars[ varName2 ] =  month;
};

<tr>
    <td>generateTomorrowDate</td>
    <td>x</td>
    <td>y</td>
</tr>
<tr>
    <td>echo</td>
    <td>${x}</td>
    <td></td>
</tr>
<tr>
    <td>echo</td>
    <td>${y}</td>
    <td></td>
</tr>

这将在明天和月份返回。

答案 3 :(得分:0)

使用selenium [html]脚本,您可以使用它。简单地说,立即获取日期/时间,并为其添加86400000毫秒(一天中的ms数= 24h * 60m * 60s * 1000ms)

<tr>
    <td>storeEval</td>
    <td>d=new Date(new Date().getTime()+86400000)</td>
    <td>tomorrow</td>
</tr>
<tr>
    <td>echo</td>
    <td>${tomorrow}</td>
    <td></td>
</tr>