Solved

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

Posted on 2014-11-04
12
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

705 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