Skip to content

How do I generate the timestamp in a specific format

This example demonstrates the use for storing the result of a javascript to a variable.

In the below solution, the current time is stored using the "storeEval" command in the "Hour Minutes Seconds TimeZoneValue " format.

Solution:

# Command Target Value
To store the response of the time stamp
1 storeEval
var timelaggingMin,timelaggingHours; var TimeZoneValue="EST"; var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; function monthNameToNum(monthname) { var month = months.indexOf(monthname); return month ? month : 0; } var newDate=new Date();newDateGMT=newDate.toGMTString(); var newDateGMTYear=Number(newDateGMT.substring(12,16)); var newDateGMTMonth=newDateGMT.substring(8,11); newDateGMTMonthNum=monthNameToNum(newDateGMTMonth); var newDateGMTDate=Number(newDateGMT.substring(5,7)); var newDateGMTHours=Number(newDateGMT.substring(17,19)); var newDateGMTMin=Number(newDateGMT.substring(20,22)); var newDateGMTSec=Number(newDateGMT.substring(23,25)); var GMTepoch=Date.UTC(newDateGMTYear, newDateGMTMonthNum, newDateGMTDate, newDateGMTHours, newDateGMTMin, newDateGMTSec); if(TimeZoneValue=="CEST") { timelaggingHours = 2; timelaggingMin = 0; } else if(TimeZoneValue=="CST") { timelaggingHours = 1; timelaggingMin = 0; } else if(TimeZoneValue=="EST") { timelaggingHours = -4; timelaggingMin = 0; } else if(TimeZoneValue=="EDT") { timelaggingHours = -5; timelaggingMin = 0; } else if(TimeZoneValue=="GMT") { timelaggingHours = 0; timelaggingMin = 0; } else if(TimeZoneValue=="IST") { timelaggingHours = 5; timelaggingMin = 30; } else if(TimeZoneValue=="ACT") { timelaggingHours = 10; timelaggingMin = 0; } var finalepoch = GMTepoch+(((1 * 60 * 60 * 1000) * timelaggingHours)+((60 * 1000) * timelaggingMin)); var finalTime=new Date(finalepoch); var finalTimeOnly=finalTime.toGMTString(); finalTimeOnly.substring(17,25)+" EST";current_time

Tips, Tricks, Gotchas & Best Practices:

  • You can use this command to generate timestamps in UTC, IST, EST, CST... by modifying the "TimeZoneValue" accordingly.
  • You can use this command to evaluate any javascript snippets for manipulating the input data as per the requirement. 
  • As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscores.


Feedback and Knowledge Base