Solved

javascript: month should be 2 chars (MM). How to prefix with "0" if 1 char is provided

Posted on 2014-11-04
12
127 Views
Last Modified: 2014-11-04
I have the following
       
var expireMonth = rq.SelectSingle("card/expire/month");
I want to check that if this month is one digit then prefix it with a "0" otherwise leave as 2 digit month
            

            
I tried the below but it doesn't prefix with "0"
        if (expireMonth .NormalizedText.length == 1)
            expireMonth = "0" + expireMonth ;

What am I doing wrong?
0
Comment
Question by:badtz7229
  • 5
  • 5
  • 2
12 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40422269
expireMonth  = ('0' + expireMonth).slice(-2) 

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40422270
expireMonth = expireMonth .NormalizedText * 1 < 10 ? ( "0" + expireMonth ) : expireMonth;

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422323
@leakim9712:
I am getting "expireMonth.NormalizedText is undefined"
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422352
Did you try my answer?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40422361
My answer depends from what you post, if it's undefined try without :
expireMonth = expireMonth * 1 < 10 ? ( "0" + expireMonth ) : expireMonth;

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422375
@Gary:
I get same error with ur solution
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 58

Expert Comment

by:Gary
ID: 40422376
How can you get the same error when NormalizedText is not in my code
0
 

Author Comment

by:badtz7229
ID: 40422398
oooh, i u see why
it is because after i'm checking if length ==1 and prefixing with "0"
i'm also checking

        if (expireMonth.NormalizedText.length != 2)
            throw "Month should be 2 characters";
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422401
// If this returns a number
var expireMonth = rq.SelectSingle("card/expire/month");

// then you just need this and nothing else
expireMonth  = ('0' + expireMonth).slice(-2) 

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422456
@Gary:

what does .slice(-2) do ?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422478
The code adds a zero to the month then takes the last two digits so

1 becomes 01 and ends as 01

10 becomes 010 and ends as 10
0
 

Author Closing Comment

by:badtz7229
ID: 40422528
this worked. thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now