Ask a Question

Set Project Level Variable for Current Date

CByler
Contributor

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 7
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


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

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

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 

 

 

Thanks, dd1984!

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")}

nmrao
Community Hero

Well.

 

No need for such big expressions, which can be simplified as below(applicable for all of them)

 

Change from:

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

 

To

${=new Date().format("yyyy-MM-dd'T'HH:mm:ss")}



Regards,
Rao.

I was just trying to give back to the community.  Way to make me rethink that...

cancel
Showing results for 
Search instead for 
Did you mean: