Skip to main content
Jan 19, 2022 10:31 AM UTC
Solved

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

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Krishnanunni
KrishnanunniAccepted solution
Level 4
Jan 19, 2022 1:14 PM UTC

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. 

3 replies

Krishnanunni
KrishnanunniAccepted solution
Level 4
Jan 19, 2022 1:14 PM UTC

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. 

Translate
shri91127Author
Jan 19, 2022 1:34 PM UTC

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!

Translate
Krishnanunni
Level 4
Jan 20, 2022 3:01 AM UTC

@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);

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?

 

Translate