Forum Discussion

CByler's avatar
CByler
Contributor
8 years ago

Set Project Level Variable for Current Date

Question (see attached screenshot)

 

I have various date formats that I test with each day.  I have variables with those formats set at the Project Level.  However, doing it in this manner requires that I must manually change the dates each day.  Is there a way for me to type in a specific variable such as @Today or @Tomorrow, so that I don't have to manually change these each day? I would need them for today, tomorrow, dates with hyphens vs forward slashes, and one with the current timestamp.  Any help is greatly appreciated :)

7 Replies

  • dd1984's avatar
    dd1984
    Occasional Contributor

    Hi, 

     

    Try with below code.

     

    ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.getTime().format("yyyy-MM-dd'T'HH:mm:ss")}

     

    I use it in my XML so that can use date with current time 

     

     

      • CByler's avatar
        CByler
        Contributor

        In case anyone is looking for examples in the future, I had several date formats I needed to test with. Below are the examples:

         

        Current yyyy/mm/dd
        ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.getTime().format("yyyy-MM-dd'T'HH:mm:ss")}

         

        Current mm/dd/yyyy
        ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.getTime().format("MM/dd/yyyy")}

         

        Current mm/dd/yyyy Plus 1 Day
        ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1);cal.getTime().format("MM/dd/yyyy")}

         

        Current mm-dd-yyyy Plus 1 Day
        ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1);cal.getTime().format("MM-dd-yyyy")}

         

        Current mm/dd/yyyy Plus 1 Day with Timestamp
        ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1);cal.getTime().format("MM/dd/yyyy'T'HH:mm:ss")}

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You can use a project setup script to initialize these values using Groovy:

    def now = new Date()
    def tomorrow = now + 1
    def futureDate = now + 1000
    
    project.setPropertyValue( "effectiveEntryDateSameDay", now.format("M/d/yy") )
    project.setPropertyValue( "effectiveEntryDateWithTimeStamp", tomorrow.format("M/d/yyyy HH:mm") )
    // etc.

    Groovy Date class reference:
    http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html

    Format strings:

    http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

    • CByler's avatar
      CByler
      Contributor

      I've never used a Project Setup Script, so I will research that today.  Thank you Helen for the quick response!