cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe

Hello

Currently we are using javascript: js: (new Date().getFullYear() + '.'+(new Date().getMonth()+1)) to fetch time period as on date. Eg. above script helps in fetching 2020.11

Now if we want to fetch Jan/Feb/March data then I will 0 appended to it eg. 2021.01/2021.02/2021.03 so is there a way to fetch this dynamically i.e if month length is 1 then append 0 ahead of it and if the months are Oct/Nov/Dec then 0 need not be appended ahead of them. Is there a way to include this if-else in BPC conversion file javascript ?

Regards

Neha

0 Likes
View Entire Topic
former_member186338
Active Contributor

Easy:

new Date().getFullYear() + '.'+('0'+(new Date().getMonth()+1)).substr(-2, 2)

Standard way...

former_member590808
Participant
0 Likes

Will this fetch 2021.01 in January without any changes ? Because I am unable to understand how adding substr() will resolve the issue. Currently with this javascript code am getting 2020.011 without substr()

former_member186338
Active Contributor
0 Likes

nehayadav0911

Sorry, but why not to test the code in any online JavaScript testing tool???

For example: https://js.do

substr(-2,2)

-2 - means 2 positions from the end of the string

2 - 2 characters

P.S. Javascript:

('0'+(new Date().getMonth()+1)).substr(-2, 2)

Is equivalent of Excel formula:

=RIGHT("0"&MONTH(NOW())+1;2)
former_member590808
Participant
0 Likes

Yes, I tried that and checked it in an online tool as well. Thanks Vadim for your help.