Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to change date format for an instance variable holding date info?

Avatar

Level 1

I have created a variable,

var NovDate = new Date();

 

This date is in MON DD YYYY Format. I want to convert this to YYYMMDD format.

 

If I write the function : var NovDate = formatDate(NovDate, "%4Y%2M%2D%2H%2M%2S"); , I'm getting an error as below:

 

 JST-310000 Error while compiling script 'WKF5041/start' line 6: NovDate.getDate is not a function.

 

Please assist.

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @shri91127 ,

You are using the same variable name with the variable declaration format (means adding the var). Javascript has a feature called variable hoisting. This will move all variable declarations to the top of the scope. So when you call formatDate function, the data type of NovDate would be string. Try removing var from the line having formatDate function call. 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 4

Hi @shri91127 ,

You are using the same variable name with the variable declaration format (means adding the var). Javascript has a feature called variable hoisting. This will move all variable declarations to the top of the scope. So when you call formatDate function, the data type of NovDate would be string. Try removing var from the line having formatDate function call. 

Avatar

Level 1

Hi @Krishnanunni ,

Thanks for your response. 

I tried the below per your solution, I'm still getting the same error.

var NovDate = new Date();

NovDate = formatDate(NovDate, "%4Y%2M%2D%2H%2M%2S");

Is this how you meant earlier?

 

I'm not sure if this is the appropriate syntax:

formatDate(NovDate, "%4Y%2M%2D%2H%2M%2S")

This works with formatDate(new Date(), "%4Y%2M%2D%2H%2M%2S");

 

Thanks!

Avatar

Level 4

@shri91127 ,

I have executed the following code and it gave me the right result, a NovDate string with the format shared.

var NovDate = new Date();
NovDate = formatDate(NovDate, "%4Y%2M%2D%2H%2N%2S");

logInfo(NovDate);

Krishnanunni_1-1642647602881.png

If you are getting error in JS activity, that could be due to error in other lines of code. Could you please try just these 3 lines of code in a separate JS activity and see if it works?