How to change date format for an instance variable holding date info? | Adobe Higher Education
Skip to main content
January 19, 2022
Respondido

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

  • January 19, 2022
  • 1 resposta
  • 1577 Visualizações

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.

Este tópico foi fechado para respostas.
Melhor resposta por Krishnanunni

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. 

1 Resposta

Krishnanunni
KrishnanunniResposta
Level 4
January 19, 2022

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. 

January 19, 2022

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!

Krishnanunni
Level 4
January 20, 2022

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