Solved

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

Posted on 2014-11-04
12
133 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

739 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